-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Expand file tree
/
Copy pathget-env.ts
More file actions
44 lines (39 loc) · 1.33 KB
/
get-env.ts
File metadata and controls
44 lines (39 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";
// Tool input schema
const GetEnvSchema = z.object({
key: z.string().describe("Specific environment variable name"),
});
// Tool configuration
const name = "get-env";
const config = {
title: "Get Environment Variable",
description:
'Returns the value of a specific environment variable. Pass the variable name via the "key" argument.',
inputSchema: GetEnvSchema,
};
/**
* Registers the 'get-env' tool.
*
* The registered tool retrieves and returns the value of a specific
* environment variable specified by the 'key' argument.
*
* @param {McpServer} server - The McpServer instance where the tool will be registered.
* @returns {void}
*/
export const registerGetEnvTool = (server: McpServer) => {
server.registerTool(name, config, async (args): Promise<CallToolResult> => {
const { key } = GetEnvSchema.parse(args);
const value = process.env[key];
// Return a clear message if the key doesn't exist rather than exposing the full env
if (value === undefined) {
return {
content: [{ type: "text", text: `Environment variable "${key}" is not set.` }],
};
}
return {
content: [{ type: "text", text: value }],
};
});
};