Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/everything/__tests__/tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,27 @@ describe('Tools', () => {

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

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

const envJson = JSON.parse(result.content[0].text);
expect(envJson.TEST_VAR_EVERYTHING).toBe('test_value');
const text = result.content[0].text;
expect(text).toBe('TEST_VAR_EVERYTHING=test_value');

delete process.env.TEST_VAR_EVERYTHING;
});

it('should return valid JSON', async () => {
it('should return valid output', async () => {
const { mockServer, handlers } = createMockServer();
registerGetEnvTool(mockServer);

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

expect(() => JSON.parse(result.content[0].text)).not.toThrow();
expect(result.content).toHaveLength(1);
expect(result.content[0].type).toBe('text');
expect(result.content[0].text).toMatch(/^PATH=/);
});
});

Expand Down
32 changes: 27 additions & 5 deletions src/everything/tools/get-env.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";

// Tool input schema
export const GetEnvSchema = z.object({
key: z.string().describe(
"The name of the environment variable to retrieve (e.g., 'PATH', 'HOME', 'USER')",
),
});

// Tool configuration
const name = "get-env";
const config = {
title: "Print Environment Tool",
description:
"Returns all environment variables, helpful for debugging MCP server configuration",
inputSchema: {},
"Returns the value of a specific environment variable, helpful for debugging MCP server configuration",
inputSchema: GetEnvSchema,
};

/**
* Registers the 'get-env' tool.
*
* The registered tool Retrieves and returns the environment variables
* of the current process as a JSON-formatted string encapsulated in a text response.
* The registered tool retrieves and returns the value of a specific
* environment variable from the current process.
*
* @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];

if (value === undefined) {
return {
content: [
{
type: "text",
text: `Environment variable '${key}' is not set.`,
},
],
};
}

return {
content: [
{
type: "text",
text: JSON.stringify(process.env, null, 2),
text: `${key}=${value}`,
},
],
};
Expand Down
Loading