Skip to content

Commit cee615d

Browse files
committed
fix(everything): limit get-env to one key
1 parent acedea0 commit cee615d

4 files changed

Lines changed: 50 additions & 11 deletions

File tree

‎src/everything/__tests__/tools.test.ts‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,29 +151,47 @@ describe('Tools', () => {
151151
});
152152

153153
describe('get-env', () => {
154-
it('should return all environment variables as JSON', async () => {
154+
it('should return a single environment variable as JSON', async () => {
155155
const { mockServer, handlers } = createMockServer();
156156
registerGetEnvTool(mockServer);
157157

158158
const handler = handlers.get('get-env')!;
159159
process.env.TEST_VAR_EVERYTHING = 'test_value';
160-
const result = await handler({});
160+
const result = await handler({ key: 'TEST_VAR_EVERYTHING' });
161161

162162
expect(result.content).toHaveLength(1);
163163
expect(result.content[0].type).toBe('text');
164164

165165
const envJson = JSON.parse(result.content[0].text);
166-
expect(envJson.TEST_VAR_EVERYTHING).toBe('test_value');
166+
expect(envJson).toEqual({
167+
key: 'TEST_VAR_EVERYTHING',
168+
value: 'test_value',
169+
found: true,
170+
});
167171

168172
delete process.env.TEST_VAR_EVERYTHING;
169173
});
170174

175+
it('should return null for missing environment variables', async () => {
176+
const { mockServer, handlers } = createMockServer();
177+
registerGetEnvTool(mockServer);
178+
179+
const handler = handlers.get('get-env')!;
180+
const result = await handler({ key: 'TEST_VAR_EVERYTHING_MISSING' });
181+
182+
expect(JSON.parse(result.content[0].text)).toEqual({
183+
key: 'TEST_VAR_EVERYTHING_MISSING',
184+
value: null,
185+
found: false,
186+
});
187+
});
188+
171189
it('should return valid JSON', async () => {
172190
const { mockServer, handlers } = createMockServer();
173191
registerGetEnvTool(mockServer);
174192

175193
const handler = handlers.get('get-env')!;
176-
const result = await handler({});
194+
const result = await handler({ key: 'PATH' });
177195

178196
expect(() => JSON.parse(result.content[0].text)).not.toThrow();
179197
});

‎src/everything/docs/features.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
- `echo` (tools/echo.ts): Echoes the provided `message: string`. Uses Zod to validate inputs.
1313
- `get-annotated-message` (tools/get-annotated-message.ts): Returns a `text` message annotated with `priority` and `audience` based on `messageType` (`error`, `success`, or `debug`); can optionally include an annotated `image`.
14-
- `get-env` (tools/get-env.ts): Returns all environment variables from the running process as pretty-printed JSON text.
14+
- `get-env` (tools/get-env.ts): Returns one requested environment variable from the running process as pretty-printed JSON text.
1515
- `get-resource-links` (tools/get-resource-links.ts): Returns an intro `text` block followed by multiple `resource_link` items. For a requested `count` (1–10), alternates between dynamic Text and Blob resources using URIs from `resources/templates.ts`.
1616
- `get-resource-reference` (tools/get-resource-reference.ts): Accepts `resourceType` (`text` or `blob`) and `resourceId` (positive integer). Returns a concrete `resource` content block (with its `uri`, `mimeType`, and data) with surrounding explanatory `text`.
1717
- `get-roots-list` (tools/get-roots-list.ts): Returns the last list of roots sent by the client.

‎src/everything/docs/structure.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ src/everything
131131
- `get-annotated-message.ts`
132132
- Registers an `annotated-message` tool which demonstrates annotated content items by emitting a primary `text` message with `annotations` that vary by `messageType` (`"error" | "success" | "debug"`), and optionally includes an annotated `image` (tiny PNG) when `includeImage` is true.
133133
- `get-env.ts`
134-
- Registers a `get-env` tool that returns the current process environment variables as formatted JSON text; useful for debugging configuration.
134+
- Registers a `get-env` tool that returns one requested environment variable as formatted JSON text; useful for debugging configuration without dumping the full process environment.
135135
- `get-resource-links.ts`
136136
- Registers a `get-resource-links` tool that returns an intro `text` block followed by multiple `resource_link` items.
137137
- `get-resource-reference.ts`

‎src/everything/tools/get-env.ts‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,52 @@
11
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
22
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3+
import { z } from "zod";
34

45
// Tool configuration
56
const name = "get-env";
7+
const GetEnvSchema = {
8+
key: z
9+
.string()
10+
.min(1)
11+
.describe("Name of the single environment variable to read."),
12+
};
613
const config = {
714
title: "Print Environment Tool",
815
description:
9-
"Returns all environment variables, helpful for debugging MCP server configuration",
10-
inputSchema: {},
16+
"Returns a single environment variable by name, helpful for debugging MCP server configuration without dumping the full process environment.",
17+
inputSchema: GetEnvSchema,
18+
annotations: {
19+
readOnlyHint: true,
20+
},
1121
};
1222

1323
/**
1424
* Registers the 'get-env' tool.
1525
*
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.
26+
* The registered tool retrieves and returns a single environment variable
27+
* from the current process as a JSON-formatted string encapsulated in a text response.
1828
*
1929
* @param {McpServer} server - The McpServer instance where the tool will be registered.
2030
* @returns {void}
2131
*/
2232
export const registerGetEnvTool = (server: McpServer) => {
2333
server.registerTool(name, config, async (args): Promise<CallToolResult> => {
34+
const key = args.key;
35+
const value = process.env[key];
36+
2437
return {
2538
content: [
2639
{
2740
type: "text",
28-
text: JSON.stringify(process.env, null, 2),
41+
text: JSON.stringify(
42+
{
43+
key,
44+
value: value ?? null,
45+
found: value !== undefined,
46+
},
47+
null,
48+
2
49+
),
2950
},
3051
],
3152
};

0 commit comments

Comments
 (0)