|
1 | 1 | import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
2 | 2 | import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; |
| 3 | +import { z } from "zod"; |
| 4 | + |
| 5 | +// Tool input schema |
| 6 | +const GetEnvSchema = z.object({ |
| 7 | + key: z.string().describe("Specific environment variable name"), |
| 8 | +}); |
3 | 9 |
|
4 | 10 | // Tool configuration |
5 | 11 | const name = "get-env"; |
6 | 12 | const config = { |
7 | | - title: "Print Environment Tool", |
| 13 | + title: "Get Environment Variable", |
8 | 14 | description: |
9 | | - "Returns all environment variables, helpful for debugging MCP server configuration", |
10 | | - inputSchema: {}, |
| 15 | + 'Returns the value of a specific environment variable. Pass the variable name via the "key" argument.', |
| 16 | + inputSchema: GetEnvSchema, |
11 | 17 | }; |
12 | 18 |
|
13 | 19 | /** |
14 | 20 | * Registers the 'get-env' tool. |
15 | 21 | * |
16 | | - * The registered tool Retrieves and returns the environment variables |
17 | | - * of the current process as a JSON-formatted string encapsulated in a text response. |
| 22 | + * The registered tool retrieves and returns the value of a specific |
| 23 | + * environment variable specified by the 'key' argument. |
18 | 24 | * |
19 | 25 | * @param {McpServer} server - The McpServer instance where the tool will be registered. |
20 | 26 | * @returns {void} |
21 | 27 | */ |
22 | 28 | export const registerGetEnvTool = (server: McpServer) => { |
23 | 29 | server.registerTool(name, config, async (args): Promise<CallToolResult> => { |
| 30 | + const { key } = GetEnvSchema.parse(args); |
| 31 | + const value = process.env[key]; |
| 32 | + |
| 33 | + // Return a clear message if the key doesn't exist rather than exposing the full env |
| 34 | + if (value === undefined) { |
| 35 | + return { |
| 36 | + content: [{ type: "text", text: `Environment variable "${key}" is not set.` }], |
| 37 | + }; |
| 38 | + } |
| 39 | + |
24 | 40 | return { |
25 | | - content: [ |
26 | | - { |
27 | | - type: "text", |
28 | | - text: JSON.stringify(process.env, null, 2), |
29 | | - }, |
30 | | - ], |
| 41 | + content: [{ type: "text", text: value }], |
31 | 42 | }; |
32 | 43 | }); |
33 | 44 | }; |
0 commit comments