Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 55 additions & 14 deletions packages/toolbox-core/src/toolbox_core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {McpHttpTransportV20241105} from './mcp/v20241105/mcp.js';
import {McpHttpTransportV20250618} from './mcp/v20250618/mcp.js';
import {McpHttpTransportV20250326} from './mcp/v20250326/mcp.js';
import {McpHttpTransportV20251125} from './mcp/v20251125/mcp.js';
import {ProtocolNegotiationError} from './errorUtils.js';
import {
BoundParams,
identifyAuthRequirements,
Expand Down Expand Up @@ -80,43 +81,55 @@ class ToolboxClient {
);
}

this.#transport = this.#createTransport(
url,
session || undefined,
protocol,
clientName,
clientVersion,
);
}

#createTransport(
url: string,
session: AxiosInstance | undefined,
protocol: Protocol,
clientName?: string,
clientVersion?: string,
): ITransport {
switch (protocol) {
case Protocol.MCP_v20241105:
this.#transport = new McpHttpTransportV20241105(
return new McpHttpTransportV20241105(
url,
session || undefined,
session,
protocol,
clientName,
clientVersion,
);
break;
case Protocol.MCP_v20250326:
this.#transport = new McpHttpTransportV20250326(
return new McpHttpTransportV20250326(
url,
session || undefined,
session,
protocol,
clientName,
clientVersion,
);
break;
case Protocol.MCP_v20250618:
this.#transport = new McpHttpTransportV20250618(
return new McpHttpTransportV20250618(
url,
session || undefined,
session,
protocol,
clientName,
clientVersion,
);
break;
case Protocol.MCP_v20251125:
this.#transport = new McpHttpTransportV20251125(
return new McpHttpTransportV20251125(
url,
session || undefined,
session,
protocol,
clientName,
clientVersion,
);
break;
default:
throw new Error(`Unsupported MCP protocol version: ${protocol}`);
}
Expand Down Expand Up @@ -214,7 +227,21 @@ class ToolboxClient {
): Promise<ToolboxTool> {
warnIfHttpAndHeaders(this.#transport.baseUrl, authTokenGetters);
const headers = await this.#resolveClientHeaders();
const manifest = await this.#transport.toolGet(name, headers);
let manifest;
try {
manifest = await this.#transport.toolGet(name, headers);
} catch (e: unknown) {
if (e instanceof ProtocolNegotiationError) {
this.#transport = this.#createTransport(
this.#transport.baseUrl,
undefined,
e.fallbackVersion as Protocol,
);
manifest = await this.#transport.toolGet(name, headers);
} else {
throw e;
}
}

if (
manifest.tools &&
Expand Down Expand Up @@ -285,7 +312,21 @@ class ToolboxClient {
const toolsetName = name || '';
const headers = await this.#resolveClientHeaders();

const manifest = await this.#transport.toolsList(toolsetName, headers);
let manifest;
try {
manifest = await this.#transport.toolsList(toolsetName, headers);
} catch (e: unknown) {
if (e instanceof ProtocolNegotiationError) {
this.#transport = this.#createTransport(
this.#transport.baseUrl,
undefined,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use the right session instead of undefined here?

e.fallbackVersion as Protocol,
);
manifest = await this.#transport.toolsList(toolsetName, headers);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the ProtocolNegotiationError class, we assume that fallback version is a string but here we're casting it as protocol. Can we keep it consistent?

} else {
throw e;
}
}
const tools: ToolboxTool[] = [];

const overallUsedAuthKeys: Set<string> = new Set();
Expand Down
10 changes: 10 additions & 0 deletions packages/toolbox-core/src/toolbox_core/errorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,13 @@ export function logApiError(baseMessage: string, error: unknown): void {
}
console.error(baseMessage, loggableDetails);
}

export class ProtocolNegotiationError extends Error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add unit tests?

fallbackVersion: string;

constructor(fallbackVersion: string) {
super(`Server requires protocol fallback to ${fallbackVersion}`);
this.name = 'ProtocolNegotiationError';
this.fallbackVersion = fallbackVersion;
}
}