-
Notifications
You must be signed in to change notification settings - Fork 6
refactor: wrap setRequestHandler for high-level servers #14
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: main
Are you sure you want to change the base?
Conversation
| if (!(error instanceof Error)) { | ||
| return { | ||
| message: stringifyNonError(error), | ||
| type: "NonError", | ||
| type: "UnknownErrorType", | ||
| platform: "javascript", | ||
| }; |
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.
I think based on their code, we know it is an McpError type I'm pretty sure. You could do a quick includes check or something here.
| return await ( | ||
| originalCallback as ( | ||
| extra: CompatibleRequestHandlerExtra, | ||
| ) => Promise<CallToolResult> | ||
| )(extra); | ||
| } else { | ||
| return await ( | ||
| originalCallback as ( | ||
| args: any, | ||
| extra: CompatibleRequestHandlerExtra, | ||
| ) => Promise<CallToolResult> |
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.
I feel like the type casting here makes this way less readable... Can we remove?
| function setupToolsCallHandlerWrapping(server: HighLevelMCPServerLike): void { | ||
| const lowLevelServer = server.server as MCPServerLike; | ||
|
|
||
| // Check if tools/call handler already exists | ||
| const existingHandler = lowLevelServer._requestHandlers.get("tools/call"); | ||
| if (existingHandler) { | ||
| const wrappedHandler = createToolsCallWrapper( | ||
| existingHandler, | ||
| lowLevelServer, | ||
| ); | ||
| lowLevelServer._requestHandlers.set("tools/call", wrappedHandler); | ||
| } | ||
|
|
||
| // Intercept future calls to setRequestHandler for tools registered after track() | ||
| const originalSetRequestHandler = | ||
| lowLevelServer.setRequestHandler.bind(lowLevelServer); | ||
|
|
||
| lowLevelServer.setRequestHandler = function ( | ||
| requestSchema: any, | ||
| handler: any, | ||
| ) { | ||
| const method = requestSchema?.shape?.method?.value; | ||
|
|
||
| // Only wrap tools/call handler | ||
| if (method === "tools/call") { | ||
| const wrappedHandler = createToolsCallWrapper(handler, lowLevelServer); | ||
| return originalSetRequestHandler(requestSchema, wrappedHandler); | ||
| } | ||
|
|
||
| // Pass through all other handlers unchanged | ||
| return originalSetRequestHandler(requestSchema, handler); | ||
| } as any; | ||
| } |
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.
I'm a bit confused as to what this whole function does lol...
| // Intercept future calls to setRequestHandler for tools registered after track() | ||
| const originalSetRequestHandler = | ||
| lowLevelServer.setRequestHandler.bind(lowLevelServer); | ||
|
|
||
| lowLevelServer.setRequestHandler = function ( | ||
| requestSchema: any, | ||
| handler: any, | ||
| ) { | ||
| const method = requestSchema?.shape?.method?.value; | ||
|
|
||
| // Only wrap tools/call handler | ||
| if (method === "tools/call") { | ||
| const wrappedHandler = createToolsCallWrapper(handler, lowLevelServer); | ||
| return originalSetRequestHandler(requestSchema, wrappedHandler); | ||
| } | ||
|
|
||
| // Pass through all other handlers unchanged | ||
| return originalSetRequestHandler(requestSchema, handler); | ||
| } as any; |
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.
I guess I just don't think this handler is ever called more than once so the rest of this function may be redundant
Summary
Modified
tracingV2.tssetupToolsCallHandlerWrapping()to interceptsetRequestHandler()calls for tools/call handlercreateToolsCallWrapper()to centralize tracing logic at the protocol handler leveladdTracingToToolCallback()to only remove context from args instead of handling full tracingTest Plan