Skip to content

Commit 2f9cc00

Browse files
committed
fix(everything): limit get-env to one key
1 parent b1e1eb1 commit 2f9cc00

4 files changed

Lines changed: 47 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
@@ -134,7 +134,7 @@ src/everything
134134
- `get-annotated-message.ts`
135135
- Registers a `get-annotated-message` tool which demonstrates content-level annotations. Emits a primary `text` message with content `annotations` (`priority`, `audience`) that vary by `messageType` (`"error" | "success" | "debug"`), and optionally includes an annotated `image` (tiny PNG) when `includeImage` is true. All tools in this server include tool-level annotations (`readOnlyHint`, `destructiveHint`, `idempotentHint`, `openWorldHint`).
136136
- `get-env.ts`
137-
- Registers a `get-env` tool that returns the current process environment variables as formatted JSON text; useful for debugging configuration.
137+
- 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.
138138
- `get-resource-links.ts`
139139
- Registers a `get-resource-links` tool that returns an intro `text` block followed by multiple `resource_link` items.
140140
- `get-resource-reference.ts`

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
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,
1118
annotations: {
1219
readOnlyHint: true,
1320
destructiveHint: false,
@@ -19,19 +26,30 @@ const config = {
1926
/**
2027
* Registers the 'get-env' tool.
2128
*
22-
* The registered tool Retrieves and returns the environment variables
23-
* of the current process as a JSON-formatted string encapsulated in a text response.
29+
* The registered tool retrieves and returns a single environment variable
30+
* from the current process as a JSON-formatted string encapsulated in a text response.
2431
*
2532
* @param {McpServer} server - The McpServer instance where the tool will be registered.
2633
* @returns {void}
2734
*/
2835
export const registerGetEnvTool = (server: McpServer) => {
2936
server.registerTool(name, config, async (args): Promise<CallToolResult> => {
37+
const key = args.key;
38+
const value = process.env[key];
39+
3040
return {
3141
content: [
3242
{
3343
type: "text",
34-
text: JSON.stringify(process.env, null, 2),
44+
text: JSON.stringify(
45+
{
46+
key,
47+
value: value ?? null,
48+
found: value !== undefined,
49+
},
50+
null,
51+
2
52+
),
3553
},
3654
],
3755
};

0 commit comments

Comments
 (0)