Skip to main content
Glama

putBase64

Upload base64-encoded content to cloud storage buckets. Specify file name, target directory, and content type for organized storage.

Instructions

上传base64编码内容到存储桶

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
base64ContentYesbase64编码的内容
fileNameYes文件名 (存在存储桶里的名称)
targetDirNo目标目录 (存在存储桶的哪个目录)
contentTypeNo内容类型,如 image/png (图片), application/pdf, application/vnd.openxmlformats-officedocument.wordprocessingml.document (文档) 等,如果base64带头部则默认自带的头部否则默认为 application/octet-stream

Implementation Reference

  • src/server.ts:148-180 (registration)
    Registration of the 'putBase64' tool, including description, Zod input schema, and inline async handler function that delegates to CosService.uploadBase64 and formats the response.
    server.tool(
      'putBase64',
      '上传base64编码内容到存储桶',
      {
        base64Content: z.string().describe('base64编码的内容'),
        fileName: z.string().describe('文件名 (存在存储桶里的名称)'),
        targetDir: z
          .string()
          .optional()
          .describe('目标目录 (存在存储桶的哪个目录)'),
        contentType: z
          .string()
          .optional()
          .describe('内容类型,如 image/png (图片), application/pdf, application/vnd.openxmlformats-officedocument.wordprocessingml.document (文档) 等,如果base64带头部则默认自带的头部否则默认为 application/octet-stream'),
      },
      async ({ base64Content, fileName, targetDir, contentType }) => {
        const res = await COSInstance.uploadBase64({
          base64Content,
          fileName,
          targetDir,
          contentType,
        });
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(res.data, null, 2),
            },
          ],
          isError: !res.isSuccess,
        };
      },
    );
  • Core handler logic for uploading base64 content to COS: validates params, processes base64 (extracts data and mime if data URI), converts to Buffer, builds COS key, performs putObject.
    async uploadBase64(params: UploadBase64Params) {
      const validParams = UploadBase64ParamsSchema.parse(params);
      let { base64Content, fileName, targetDir = '' } = validParams;
      
      try {
        // 构建COS路径
        const cosPath = this.buildCosPath(fileName, targetDir);
    
        // 处理base64
        let { base64Data, contentType } = this.processBase64(base64Content, validParams.contentType);
    
        // 将base64转换为Buffer
        const buffer = Buffer.from(base64Data, 'base64');
    
        // 上传buffer内容
        const cosParams: COS.PutObjectParams = {
          Bucket: this.bucket,
          Region: this.region,
          Key: cosPath,
          Body: buffer,
          ContentType: contentType || 'application/octet-stream',
        };
    
        const result = await this.cos.putObject(cosParams);
    
        return {
          isSuccess: true,
          message: '上传成功',
          data: result,
        };
      } catch (error) {
        return {
          isSuccess: false,
          message: '上传失败',
          data: error,
        };
      }
    }
  • Zod schema for UploadBase64Params used by the uploadBase64 method.
    export const UploadBase64ParamsSchema = z.object({
      base64Content: z.string(),
      fileName: z.string(),
      targetDir: z.string().optional(),
      contentType: z.string().optional()
    });
    export type UploadBase64Params = z.infer<typeof UploadBase64ParamsSchema>;
  • Helper method to process base64 string: handles data URI prefixes, extracts clean base64 data and MIME type.
      private processBase64(base64 = '', contentType = '') {
      // 基础验证
      if (typeof base64 !== 'string') {
        throw new Error('base64参数必须是字符串');
      }
      
      if (typeof contentType !== 'string') {
        throw new Error('contentType参数必须是字符串');
      }
      
      let base64Data = base64;
      let finalContentType = contentType;
      
      // 检查base64是否包含数据头
      const dataUriRegex = /^data:([^;]+);base64,/;
      const match = base64.match(dataUriRegex);
      
      if (match) {
        // 如果base64有数据头,提取纯数据和contentType
        const headerEndIndex = base64.indexOf(',') + 1;
        base64Data = base64.substring(headerEndIndex);
        
        // 如果没有传递contentType,则从base64头中提取
        if (!contentType.trim()) {
          finalContentType = match[1];
        }
      }
      
      return {
        base64Data: base64Data.trim(),
        contentType: finalContentType.trim()
      };
    }
  • Helper method to build the COS object key/path from filename and optional target directory.
    private buildCosPath(fileName: string, targetDir?: string): string {
      if (!targetDir) {
        return fileName;
      }
      
      // 规范化目标目录:移除头尾斜杠
      const normalizedDir = targetDir.replace(/^\/+|\/+$/g, '');
      return normalizedDir ? `${normalizedDir}/${fileName}` : fileName;
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. While '上传' (upload) implies a write/mutation operation, the description doesn't disclose important behavioral aspects like authentication requirements, error conditions, rate limits, whether the operation overwrites existing files, or what happens on success/failure. For a mutation tool with zero annotation coverage, this is a significant gap.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient Chinese sentence that directly states the tool's purpose without any unnecessary words or structural complexity. It's appropriately sized and front-loaded with the core functionality.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a mutation tool with no annotations and no output schema, the description is inadequate. It doesn't explain what happens after upload (success response, error handling), doesn't mention authentication or permission requirements, and provides no context about the storage system. Given the tool's complexity as a write operation, more behavioral context is needed.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all four parameters thoroughly. The description doesn't add any meaningful parameter semantics beyond what's already in the schema - it doesn't explain parameter interactions, provide examples, or clarify edge cases. With complete schema coverage, the baseline of 3 is appropriate.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('上传' meaning 'upload') and resource ('base64编码内容到存储桶' meaning 'base64 encoded content to storage bucket'), providing a specific verb+resource combination. However, it doesn't distinguish this tool from its sibling tools like putBuffer, putObject, putObjectSourceUrl, and putString, which all appear to be alternative upload methods.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. With multiple sibling tools for uploading (putBuffer, putObject, putObjectSourceUrl, putString), there's no indication of when this base64-specific upload method is preferred or what distinguishes it from other upload methods.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/xiaomizhoubaobei/MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server