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}`);
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(
52
host: IAgentServerToolHost,