src/vs/platform/agentHost/node/claude/claudeServerToolMcpServer.ts

71 LOC · 71 covered · 0 uncovered · 10 ranges · 363 concepts · 6 introducers · 204 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- claudeServerToolMcpServer.ts ×2
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import type { McpSdkServerConfigWithInstance } from '@anthropic-ai/claude-agent-sdk';
7 > import type { IAgentServerToolHost } from '../../common/agentServerTools.js';
8 > import type { IClaudeAgentSdkService } from './claudeAgentSdkService.js';
9 > import { jsonSchemaToZodRawShape } from './clientTools/claudeJsonSchemaToZod.js';
10 >
11 > /**
12 > * Name of the single in-process MCP server that surfaces the agent host's
13 > * server tools (feedback "comments" today, more in the future) to the Claude
14 > * SDK via `Options.mcpServers['host']`.
15 > *
16 > * Distinct from the client-tool server ({@link CLAUDE_CLIENT_MCP_SERVER_NAME})
17 > * because the two have opposite execution models: client tools round-trip to
18 > * the workbench, whereas server tools execute in-process against the session's
19 > * own state channels.
20 > */
21 > export const CLAUDE_SERVER_TOOL_MCP_SERVER_NAME = 'host';
22 >
23 > /**
24 > * Prefix the given server tool names into the SDK names the model sees
25 > * (`mcp__<server>__<tool>`). The Anthropic SDK prefixes every in-process MCP
26 > * tool with `mcp__<serverName>__`, so callers feed the result into
27 > * `Options.allowedTools` to auto-approve the server tools without prompting —
28 > * they only read or mutate the session's own server-held state and never touch
29 > * the workspace, shell, or network.
30 > *
31 > * Takes the names from the host (rather than a static list) so any contributed
32 > * server tool is auto-approved automatically.
33 > */
34 > export function serverToolAllowList(toolNames: readonly string[]): string[] {
35 > return toolNames.map(name => `mcp__${CLAUDE_SERVER_TOOL_MCP_SERVER_NAME}__${name}`); claudeServerToolMcpServer.ts ×1
36 > }
38 > /**
39 > * Build the per-session in-process MCP server that surfaces the agent host's
40 > * server tools to the Claude SDK.
41 > *
42 > * Unlike {@link buildClientToolMcpServer}, these tools do not round-trip to
43 > * the workbench: each handler executes synchronously in-process via
44 > * {@link IAgentServerToolHost.executeTool} against the session's own state and
45 > * returns the textual tool result directly. A throwing host (invalid
46 > * arguments, unknown tool) degrades to an `isError` result rather than
47 > * rejecting, mirroring the client-tool server.
48 > *
49 > * Pure factory — no SDK loading beyond the injected {@link IClaudeAgentSdkService}.
50 > */
51 > export async function buildServerToolMcpServer( claudeServerToolMcpServer.ts ×2
52 > host: IAgentServerToolHost,
53 > sessionUri: string,
54 > sdk: IClaudeAgentSdkService,
55 > ): Promise<McpSdkServerConfigWithInstance> {
56 > const tools = await Promise.all(host.definitions.map(def => sdk.tool(
57 > def.name,
58 > def.description ?? '',
59 > jsonSchemaToZodRawShape(def.inputSchema),
60 > async args => {
62 > const text = await host.executeTool(sessionUri, def.name, args);
63 > return { content: [{ type: 'text' as const, text }] }; claudeServerToolMcpServer.ts ×1
64 > } catch (error) { claudeServerToolMcpServer.ts ×3
65 > const message = error instanceof Error ? error.message : String(error); claudeServerToolMcpServer.ts ×1
66 > return { content: [{ type: 'text' as const, text: message }], isError: true };
67 > }
70 > return sdk.createSdkMcpServer({ name: CLAUDE_SERVER_TOOL_MCP_SERVER_NAME, tools });
71 > }