chore: tool definition without zod (#873)
This commit is contained in:
@@ -2,3 +2,6 @@
|
||||
../log.js
|
||||
../manualPromise.js
|
||||
../httpServer.js
|
||||
|
||||
[proxyBackend.ts]
|
||||
../package.js
|
||||
@@ -15,12 +15,12 @@
|
||||
*/
|
||||
|
||||
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
||||
|
||||
import { z } from 'zod';
|
||||
import { ServerBackend, ToolResponse, ToolSchema } from './server.js';
|
||||
import { defineTool, Tool } from '../tools/tool.js';
|
||||
import { packageJSON } from '../package.js';
|
||||
import { zodToJsonSchema } from 'zod-to-json-schema';
|
||||
|
||||
import { logUnhandledError } from '../log.js';
|
||||
import { packageJSON } from '../package.js';
|
||||
import { ToolDefinition, ServerBackend, ToolResponse } from './server.js';
|
||||
|
||||
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
||||
|
||||
@@ -40,8 +40,8 @@ export class ProxyBackend implements ServerBackend {
|
||||
|
||||
private _clientFactories: ClientFactoryList;
|
||||
private _currentClient: Client | undefined;
|
||||
private _contextSwitchTool: Tool<any>;
|
||||
private _tools: ToolSchema<any>[] = [];
|
||||
private _contextSwitchTool: ToolDefinition;
|
||||
private _tools: ToolDefinition[] = [];
|
||||
private _server: Server | undefined;
|
||||
|
||||
constructor(clientFactories: ClientFactoryList) {
|
||||
@@ -54,20 +54,20 @@ export class ProxyBackend implements ServerBackend {
|
||||
await this._setCurrentClient(this._clientFactories[0]);
|
||||
}
|
||||
|
||||
tools(): ToolSchema<any>[] {
|
||||
tools(): ToolDefinition[] {
|
||||
if (this._clientFactories.length === 1)
|
||||
return this._tools;
|
||||
return [
|
||||
...this._tools,
|
||||
this._contextSwitchTool.schema,
|
||||
this._contextSwitchTool,
|
||||
];
|
||||
}
|
||||
|
||||
async callTool(schema: ToolSchema<any>, rawArguments: any): Promise<ToolResponse> {
|
||||
if (schema.name === this._contextSwitchTool.schema.name)
|
||||
async callTool(name: string, rawArguments: any): Promise<ToolResponse> {
|
||||
if (name === this._contextSwitchTool.name)
|
||||
return this._callContextSwitchTool(rawArguments);
|
||||
const result = await this._currentClient!.callTool({
|
||||
name: schema.name,
|
||||
name,
|
||||
arguments: rawArguments,
|
||||
});
|
||||
return result as unknown as ToolResponse;
|
||||
@@ -95,39 +95,28 @@ export class ProxyBackend implements ServerBackend {
|
||||
}
|
||||
}
|
||||
|
||||
private _defineContextSwitchTool(): Tool<any> {
|
||||
return defineTool({
|
||||
capability: 'core',
|
||||
|
||||
schema: {
|
||||
name: 'browser_connect',
|
||||
private _defineContextSwitchTool(): ToolDefinition {
|
||||
return {
|
||||
name: 'browser_connect',
|
||||
description: [
|
||||
'Connect to a browser using one of the available methods:',
|
||||
...this._clientFactories.map(factory => `- "${factory.name}": ${factory.description}`),
|
||||
].join('\n'),
|
||||
inputSchema: zodToJsonSchema(z.object({
|
||||
name: z.enum(this._clientFactories.map(factory => factory.name) as [string, ...string[]]).default(this._clientFactories[0].name).describe('The method to use to connect to the browser'),
|
||||
}), { strictUnions: true }) as ToolDefinition['inputSchema'],
|
||||
annotations: {
|
||||
title: 'Connect to a browser context',
|
||||
description: [
|
||||
'Connect to a browser using one of the available methods:',
|
||||
...this._clientFactories.map(factory => `- "${factory.name}": ${factory.description}`),
|
||||
].join('\n'),
|
||||
inputSchema: z.object({
|
||||
name: z.enum(this._clientFactories.map(factory => factory.name) as [string, ...string[]]).default(this._clientFactories[0].name).describe('The method to use to connect to the browser'),
|
||||
}),
|
||||
type: 'readOnly',
|
||||
readOnlyHint: true,
|
||||
openWorldHint: false,
|
||||
},
|
||||
|
||||
async handle() {
|
||||
throw new Error('Unreachable');
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
private async _setCurrentClient(factory: ClientFactory) {
|
||||
await this._currentClient?.close();
|
||||
this._currentClient = await factory.create(this._server!);
|
||||
const tools = await this._currentClient.listTools();
|
||||
this._tools = tools.tools.map(tool => ({
|
||||
name: tool.name,
|
||||
title: tool.title ?? '',
|
||||
description: tool.description ?? '',
|
||||
inputSchema: tool.inputSchema ?? z.object({}),
|
||||
type: tool.annotations?.readOnlyHint ? 'readOnly' as const : 'destructive' as const,
|
||||
}));
|
||||
this._tools = tools.tools;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,14 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { z } from 'zod';
|
||||
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
||||
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
||||
import { zodToJsonSchema } from 'zod-to-json-schema';
|
||||
import { ManualPromise } from '../manualPromise.js';
|
||||
import { logUnhandledError } from '../log.js';
|
||||
|
||||
import type { ImageContent, TextContent } from '@modelcontextprotocol/sdk/types.js';
|
||||
import type { ImageContent, TextContent, Tool } from '@modelcontextprotocol/sdk/types.js';
|
||||
import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
|
||||
export type { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
||||
|
||||
@@ -36,22 +34,14 @@ export type ToolResponse = {
|
||||
isError?: boolean;
|
||||
};
|
||||
|
||||
export type ToolSchema<Input extends z.Schema> = {
|
||||
name: string;
|
||||
title: string;
|
||||
description: string;
|
||||
inputSchema: Input;
|
||||
type: 'readOnly' | 'destructive';
|
||||
};
|
||||
|
||||
export type ToolHandler = (toolName: string, params: any) => Promise<ToolResponse>;
|
||||
export type ToolDefinition = Tool;
|
||||
|
||||
export interface ServerBackend {
|
||||
name: string;
|
||||
version: string;
|
||||
initialize?(server: Server): Promise<void>;
|
||||
tools(): ToolSchema<any>[];
|
||||
callTool(schema: ToolSchema<any>, rawArguments: any): Promise<ToolResponse>;
|
||||
tools(): ToolDefinition[];
|
||||
callTool(name: string, rawArguments: any): Promise<ToolResponse>;
|
||||
serverClosed?(): void;
|
||||
}
|
||||
|
||||
@@ -73,17 +63,7 @@ export function createServer(backend: ServerBackend, runHeartbeat: boolean): Ser
|
||||
|
||||
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||
const tools = backend.tools();
|
||||
return { tools: tools.map(tool => ({
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
inputSchema: tool.inputSchema instanceof z.ZodType ? zodToJsonSchema(tool.inputSchema) : tool.inputSchema,
|
||||
annotations: {
|
||||
title: tool.title,
|
||||
readOnlyHint: tool.type === 'readOnly',
|
||||
destructiveHint: tool.type === 'destructive',
|
||||
openWorldHint: true,
|
||||
},
|
||||
})) };
|
||||
return { tools };
|
||||
});
|
||||
|
||||
let heartbeatRunning = false;
|
||||
@@ -100,12 +80,12 @@ export function createServer(backend: ServerBackend, runHeartbeat: boolean): Ser
|
||||
isError: true,
|
||||
});
|
||||
const tools = backend.tools();
|
||||
const tool = tools.find(tool => tool.name === request.params.name) as ToolSchema<any>;
|
||||
const tool = tools.find(tool => tool.name === request.params.name);
|
||||
if (!tool)
|
||||
return errorResult(`Error: Tool "${request.params.name}" not found`);
|
||||
|
||||
try {
|
||||
return await backend.callTool(tool, request.params.arguments || {});
|
||||
return await backend.callTool(tool.name, request.params.arguments || {});
|
||||
} catch (error) {
|
||||
return errorResult(String(error));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user