getObject
Download files from cloud storage buckets by specifying the file path. This tool enables retrieval of stored objects for access or processing.
Instructions
下载存储桶内的文件
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| objectKey | Yes | 文件的路径 |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"objectKey": {
"description": "文件的路径",
"type": "string"
}
},
"required": [
"objectKey"
],
"type": "object"
}
Implementation Reference
- src/server.ts:272-296 (registration)Registration of the MCP tool 'getObject' using server.tool(), including schema and inline handler.server.tool( 'getObject', '下载存储桶内的文件', { objectKey: z.string().describe('文件的路径'), }, async ({ objectKey = '/' }) => { const res = await COSInstance.getObject(objectKey); if (!res.isSuccess) { return { content: [ { type: 'text', text: JSON.stringify(res.data, null, 2), }, ], isError: true }; } return { content: [res.data] as any, isError: false, }; }, );
- src/server.ts:278-295 (handler)The handler function that executes the 'getObject' tool logic, delegating to COSInstance.getObject and formatting the MCP response.async ({ objectKey = '/' }) => { const res = await COSInstance.getObject(objectKey); if (!res.isSuccess) { return { content: [ { type: 'text', text: JSON.stringify(res.data, null, 2), }, ], isError: true }; } return { content: [res.data] as any, isError: false, }; },
- src/server.ts:275-277 (schema)Zod input schema defining the 'objectKey' parameter for the 'getObject' tool.{ objectKey: z.string().describe('文件的路径'), },
- Supporting method in CosService that performs the actual COS getObject operation and converts the result to MCP-compatible content format (image/audio/text).async getObject(objectKey = '/') { try { // 下载文件 const cosParams: COS.GetObjectParams = { Bucket: this.bucket, Region: this.region, Key: objectKey, }; const result = await this.cos.getObject(cosParams); // 统一处理 buffer const buffer = Buffer.isBuffer(result.Body) ? result.Body : Buffer.from(result.Body ?? ''); // 获取 Content-Type,统一小写 let contentType = result.headers && (result.headers['content-type'] || result.headers['Content-Type']); contentType = typeof contentType === 'string' ? contentType.toLowerCase() : ''; let mcpData; if (contentType.startsWith('image/')) { mcpData = { type: 'image', data: buffer.toString('base64'), mimeType: contentType }; } else if (contentType.startsWith('audio/')) { mcpData = { type: 'audio', data: buffer.toString('base64'), mimeType: contentType }; } else if (contentType.startsWith('text/') || TEXT_TYPES.includes(contentType)) { mcpData = { type: 'text', text: buffer.toString('utf-8') }; } else { mcpData = { type: 'text', text: buffer.toString('base64') }; } return { isSuccess: true, message: '下载文件成功', data: mcpData, }; } catch (error) { return { isSuccess: false, message: '下载文件失败', data: error instanceof Error ? error.message : error, }; } }