fix(proxy): properly forward root requests and client metadata (#865)

This commit is contained in:
Yury Semikhatsky
2025-08-12 01:17:45 -07:00
committed by GitHub
parent ab0ecc4075
commit 1fb2878271
5 changed files with 106 additions and 55 deletions

View File

@@ -16,12 +16,13 @@
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { ListRootsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
import { BrowserContextFactory } from './browserContextFactory.js';
import { BrowserServerBackend } from './browserServerBackend.js';
import { InProcessTransport } from './mcp/inProcessTransport.js';
import * as mcpServer from './mcp/server.js';
import { packageJSON } from './package.js';
import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
import type { FullConfig } from './config.js';
import type { ClientFactory } from './mcp/proxyBackend.js';
@@ -39,13 +40,20 @@ export class InProcessClientFactory implements ClientFactory {
this._config = config;
}
async create(): Promise<Client> {
const client = new Client({
name: this.name,
version: packageJSON.version
});
const server = mcpServer.createServer(new BrowserServerBackend(this._config, this._contextFactory), false);
await client.connect(new InProcessTransport(server));
async create(server: Server): Promise<Client> {
const client = new Client(server.getClientVersion() ?? { name: 'unknown', version: 'unknown' });
const clientCapabilities = server.getClientCapabilities();
if (clientCapabilities)
client.registerCapabilities(clientCapabilities);
if (clientCapabilities?.roots) {
client.setRequestHandler(ListRootsRequestSchema, async () => {
return await server.listRoots();
});
}
const delegate = mcpServer.createServer(new BrowserServerBackend(this._config, this._contextFactory), false);
await client.connect(new InProcessTransport(delegate));
await client.ping();
return client;
}

View File

@@ -29,7 +29,7 @@ type NonEmptyArray<T> = [T, ...T[]];
export type ClientFactory = {
name: string;
description: string;
create(): Promise<Client>;
create(server: Server): Promise<Client>;
};
export type ClientFactoryList = NonEmptyArray<ClientFactory>;
@@ -42,6 +42,7 @@ export class ProxyBackend implements ServerBackend {
private _currentClient: Client | undefined;
private _contextSwitchTool: Tool<any>;
private _tools: ToolSchema<any>[] = [];
private _server: Server | undefined;
constructor(clientFactories: ClientFactoryList) {
this._clientFactories = clientFactories;
@@ -49,6 +50,7 @@ export class ProxyBackend implements ServerBackend {
}
async initialize(server: Server): Promise<void> {
this._server = server;
await this._setCurrentClient(this._clientFactories[0]);
}
@@ -118,7 +120,7 @@ export class ProxyBackend implements ServerBackend {
private async _setCurrentClient(factory: ClientFactory) {
await this._currentClient?.close();
this._currentClient = await factory.create();
this._currentClient = await factory.create(this._server!);
const tools = await this._currentClient.listTools();
this._tools = tools.tools.map(tool => ({
name: tool.name,

View File

@@ -76,7 +76,7 @@ export function createServer(backend: ServerBackend, runHeartbeat: boolean): Ser
return { tools: tools.map(tool => ({
name: tool.name,
description: tool.description,
inputSchema: zodToJsonSchema(tool.inputSchema),
inputSchema: tool.inputSchema instanceof z.ZodType ? zodToJsonSchema(tool.inputSchema) : tool.inputSchema,
annotations: {
title: tool.title,
readOnlyHint: tool.type === 'readOnly',