From 29669c4d07fcc1ea009ffd7f3daaf575cfdb3107 Mon Sep 17 00:00:00 2001 From: gengruilin Date: Fri, 6 Nov 2026 03:52:23 +0800 Subject: [PATCH] refactor(mcp-client): resolve on-demand tool loading through the host schemas The server schemas moved to config.ts, where the MCP client host and the entry share them, so `toolLoading` and `catalogDescriptionMaxLength` join the stdio and Streamable HTTP schemas and input types there; the merge already passes the manager entry's `toolLoading` to the host with the other client fields. The tool-loading spec reads instance status from the status module, as the entry no longer exports it, and the API catalog records the two fields. --- .../extensions/tool-cordis/src/api-catalog.ts | 4 ++-- packages/mcp/mcp-client/src/config.ts | 15 +++++++++++---- .../mcp/mcp-client/tests/tool-loading.spec.ts | 3 ++- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/extensions/tool-cordis/src/api-catalog.ts b/packages/extensions/tool-cordis/src/api-catalog.ts index 03d2fa5ef8..0b8a6f6312 100644 --- a/packages/extensions/tool-cordis/src/api-catalog.ts +++ b/packages/extensions/tool-cordis/src/api-catalog.ts @@ -9506,7 +9506,7 @@ export const TYPE_API: readonly TypeApiEntry[] = [ }, { name: 'StdioConfig', - declaration: 'export interface StdioConfig {\n transport: \'stdio\';\n serverName: string;\n command: string;\n args: string[];\n env: Record;\n cwd: string;\n toolCallTimeoutMs: number;\n startupTimeoutMs?: number;\n closeTimeoutMs: number;\n expandTemplates: boolean;\n failOnStartupError: boolean;\n maxInstructionBytes?: number;\n reconnect?: ReconnectConfig;\n}', + declaration: 'export interface StdioConfig {\n transport: \'stdio\';\n serverName: string;\n command: string;\n args: string[];\n env: Record;\n cwd: string;\n toolCallTimeoutMs: number;\n startupTimeoutMs?: number;\n closeTimeoutMs: number;\n expandTemplates: boolean;\n failOnStartupError: boolean;\n maxInstructionBytes?: number;\n reconnect?: ReconnectConfig;\n toolLoading: \'eager\' | \'on-demand\';\n catalogDescriptionMaxLength: number;\n}', }, { name: 'StopShareRequest', @@ -9530,7 +9530,7 @@ export const TYPE_API: readonly TypeApiEntry[] = [ }, { name: 'StreamableHttpConfig', - declaration: 'export interface StreamableHttpConfig {\n oauth?: McpOAuthConfig;\n transport: \'streamable-http\';\n serverName: string;\n url: string;\n headers: Record;\n toolCallTimeoutMs: number;\n startupTimeoutMs?: number;\n closeTimeoutMs: number;\n expandTemplates: boolean;\n failOnStartupError: boolean;\n maxInstructionBytes?: number;\n reconnect?: ReconnectConfig;\n}', + declaration: 'export interface StreamableHttpConfig {\n oauth?: McpOAuthConfig;\n transport: \'streamable-http\';\n serverName: string;\n url: string;\n headers: Record;\n toolCallTimeoutMs: number;\n startupTimeoutMs?: number;\n closeTimeoutMs: number;\n expandTemplates: boolean;\n failOnStartupError: boolean;\n maxInstructionBytes?: number;\n reconnect?: ReconnectConfig;\n toolLoading: \'eager\' | \'on-demand\';\n catalogDescriptionMaxLength: number;\n}', }, { name: 'StreamChunk', diff --git a/packages/mcp/mcp-client/src/config.ts b/packages/mcp/mcp-client/src/config.ts index f8c1ea253e..e96d2f210a 100644 --- a/packages/mcp/mcp-client/src/config.ts +++ b/packages/mcp/mcp-client/src/config.ts @@ -19,16 +19,19 @@ const DEFAULT_TOOL_CALL_TIMEOUT_MS = 60_000 /** Default budget for transport closure and local cleanup (ms). */ const DEFAULT_CLOSE_TIMEOUT_MS = 5_000 +/** Default cap on each tool summary in an on-demand server's prompt catalog. */ +const DEFAULT_CATALOG_DESCRIPTION_MAX_LENGTH = 160 + /** Valid `serverName`, kept below the public tool-name budget. */ export const SERVER_NAME_PATTERN = /^[A-Za-z0-9_-]{1,32}$/ /** {@link StdioConfig} before defaults. */ -type StdioConfigInput = Omit - & Partial> +type StdioConfigInput = Omit + & Partial> /** {@link StreamableHttpConfig} before defaults. */ -type StreamableHttpConfigInput = Omit - & Partial> +type StreamableHttpConfigInput = Omit + & Partial> & { oauth?: McpOAuthConfigInput } /** One server's configuration before defaults. */ @@ -61,6 +64,8 @@ export const StdioSchema = z.object({ failOnStartupError: z.boolean().default(false), maxInstructionBytes: z.number().step(1).min(1).default(DEFAULT_MAX_INSTRUCTION_BYTES), reconnect: ReconnectSchema, + toolLoading: z.union(['eager', 'on-demand']).default('eager'), + catalogDescriptionMaxLength: z.natural().min(4).default(DEFAULT_CATALOG_DESCRIPTION_MAX_LENGTH), }) as z /** Schema of {@link StreamableHttpConfig}. */ @@ -77,6 +82,8 @@ export const StreamableHttpSchema = z.object({ failOnStartupError: z.boolean().default(false), maxInstructionBytes: z.number().step(1).min(1).default(DEFAULT_MAX_INSTRUCTION_BYTES), reconnect: ReconnectSchema, + toolLoading: z.union(['eager', 'on-demand']).default('eager'), + catalogDescriptionMaxLength: z.natural().min(4).default(DEFAULT_CATALOG_DESCRIPTION_MAX_LENGTH), }) as z /* jscpd:ignore-end */ diff --git a/packages/mcp/mcp-client/tests/tool-loading.spec.ts b/packages/mcp/mcp-client/tests/tool-loading.spec.ts index 3f7fcf6ed0..8711fc6c33 100644 --- a/packages/mcp/mcp-client/tests/tool-loading.spec.ts +++ b/packages/mcp/mcp-client/tests/tool-loading.spec.ts @@ -8,6 +8,7 @@ import { executeToolFixture } from '@deepseek-ai/dsh-tool-testkit' import * as NativeToolExecutor from '@deepseek-ai/dsh-tool-executor-native' import ToolRuntime, { defineTool } from '@deepseek-ai/dsh-tools' import * as McpClient from '../src/index.ts' +import { connectionStatusIn } from '../src/status.ts' import { McpToolLoader } from '../src/tool-loading.ts' import { McpToolRegistrationError, syncTools, type ToolBridgeOptions, type ToolDisposers } from '../src/tools.ts' import { startHttpMcpFixture } from './http-fixture.ts' @@ -248,7 +249,7 @@ it('loads and calls an on-demand server\'s tool through the plugin and withdraws toolLoading: 'on-demand', failOnStartupError: true, reconnect: { enabled: false }, })) - expect(McpClient.connectionStatusOf(fiber)!.current()).toMatchObject({ state: 'connected', toolCount: 1 }) + expect(connectionStatusIn(fiber)!.current()).toMatchObject({ state: 'connected', toolCount: 1 }) expect(ctx.tools.get('mcp__demo__ping', agent)).toBeUndefined() const prompt = await ctx.systemPrompt.assemble({ scope: agent }) expect(prompt.sections.map(section => section.text)).toContain([