get_object
Retrieve object content from an S3-compatible storage bucket by specifying the bucket name and object key, enabling access to stored files and data.
Instructions
Get object content from a bucket
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| bucket | Yes | Bucket name | |
| key | Yes | Object key |
Implementation Reference
- src/server.ts:100-132 (handler)The handler function for the 'get_object' MCP tool. Retrieves object using S3Client.getObject, handles errors, formats content as text, with special trimming for markdown files.async ({ bucket, key }: GetObjectParams) => { const object = await this.s3Client.getObject(bucket, key); if (!object) { return { content: [ { type: "text", text: "Object not found", }, ], isError: true, }; } // Ensure we're returning a proper string let content = typeof object === "string" ? object : JSON.stringify(object); // For markdown files, ensure proper formatting if (key.endsWith(".md")) { // Remove any potential BOM or special characters content = content.replace(/^\uFEFF/, "").trim(); } return { content: [ { type: "text", text: content, }, ], }; }
- src/server.ts:97-99 (schema)Zod input schema for the get_object tool defining required parameters bucket and key.bucket: z.string().describe("Bucket name"), key: z.string().describe("Object key"), },
- src/server.ts:93-99 (registration)Registration of the 'get_object' tool on the MCP server, including name, description, and input schema.this.server.tool( "get_object", "Get object content from a bucket", { bucket: z.string().describe("Bucket name"), key: z.string().describe("Object key"), },
- src/server.ts:11-14 (schema)TypeScript type definition for GetObjectParams used to type the handler input.interface GetObjectParams { bucket: string; key: string; }
- src/s3Client.ts:50-116 (helper)Core helper method in S3Client that executes the AWS GetObjectCommand, streams and converts body to UTF-8 text, cleans content for text files, prettifies JSON, handles errors.async getObject(bucket: string, key: string) { try { const command = new GetObjectCommand({ Bucket: bucket, Key: key, }); const response = await this.client.send(command); if (!response.Body) { return null; } // Convert the response body to string const bodyStream = response.Body as any; const chunks: Uint8Array[] = []; for await (const chunk of bodyStream) { chunks.push(chunk); } const buffer = Buffer.concat(chunks); const text = buffer.toString("utf-8"); // Handle different file types const extension = key.split(".").pop()?.toLowerCase(); // Text-based files that should be cleaned const textFileExtensions = [ "md", "txt", "json", "yaml", "yml", "xml", "html", "css", "js", "ts", "py", "sh", "bash", ]; if (textFileExtensions.includes(extension || "")) { // Clean the text content const cleanedText = text .replace(/^\uFEFF/, "") // Remove BOM .trim(); // Remove extra whitespace // Special handling for JSON files if (extension === "json") { try { // Validate and format JSON const parsed = JSON.parse(cleanedText); return JSON.stringify(parsed, null, 2); } catch { // If JSON parsing fails, return as is return cleanedText; } } return cleanedText; } // For binary or unknown file types, return as is return text; } catch (error) { console.error("Error getting object:", error); return null; } }