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 { CallToolResult } from '@modelcontextprotocol/sdk/types.js';
8
>
import type { ToolDefinition } from '../../../common/state/protocol/state.js';
9
>
import type { IClaudeAgentSdkService } from '../claudeAgentSdkService.js';
10
>
import { jsonSchemaToZodRawShape } from './claudeJsonSchemaToZod.js';
11
>
12
>
/**
13
>
* Anthropic SDK contract: the in-process MCP `tool()` handler receives a
14
>
* `RequestHandlerExtra` whose `_meta` carries the originating
15
>
* `tool_use_id` under this namespaced key. Verified empirically against
17
>
* or renames this field, the handler returns an error result instead of
18
>
* silently deadlocking — see {@link extractToolUseId}.
19
>
*/
20
>
const TOOL_USE_ID_META_KEY = 'claudecode/toolUseId';
21
>
22
>
/**
23
>
* Build the per-session in-process MCP server that surfaces the workbench
24
>
* client's {@link ToolDefinition}s to the Claude SDK via
25
>
* `Options.mcpServers['client']`.
26
>
*
27
>
* Each tool's handler reads the originating `tool_use_id` from the SDK's
28
>
* `extra._meta["claudecode/toolUseId"]` and delegates to `awaitResult`,
29
>
* which is expected to return a promise that settles when the workbench
30
>
* echoes a completion (typically via a parked deferred owned by the
31
>
* session). Keeping that plumbing behind a callback lets this factory
32
>
* stay ignorant of how the host tracks in-flight tool calls.
33
>
*
34
>
* Pure factory — no SDK loading, no I/O.
35
>
*/
36
export async function buildClientToolMcpServer(
37
snapshot: readonly ToolDefinition[],