-
Notifications
You must be signed in to change notification settings - Fork 19
feat(core): implement support for Stateless MCP (SEP-2575) #393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: mcp-v202606
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
|
@@ -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}`); | ||
| } | ||
|
|
@@ -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 && | ||
|
|
@@ -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, | ||
| e.fallbackVersion as Protocol, | ||
| ); | ||
| manifest = await this.#transport.toolsList(toolsetName, headers); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,3 +40,13 @@ export function logApiError(baseMessage: string, error: unknown): void { | |
| } | ||
| console.error(baseMessage, loggableDetails); | ||
| } | ||
|
|
||
| export class ProtocolNegotiationError extends Error { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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?