@@ -8,6 +8,8 @@ import { mongoLogId } from "mongodb-log-writer";
8
8
import { ObjectId } from "mongodb" ;
9
9
import { Telemetry } from "./telemetry/telemetry.js" ;
10
10
import { UserConfig } from "./config.js" ;
11
+ import { CallToolRequestSchema , CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
12
+ import assert from "assert" ;
11
13
12
14
export interface ServerOptions {
13
15
session : Session ;
@@ -33,6 +35,29 @@ export class Server {
33
35
this . registerTools ( ) ;
34
36
this . registerResources ( ) ;
35
37
38
+ // This is a workaround for an issue we've seen with some models, where they'll see that everything in the `arguments`
39
+ // object is optional, and then not pass it at all. However, the MCP server expects the `arguments` object to be if
40
+ // the tool accepts any arguments, even if they're all optional.
41
+ //
42
+ // see: https://github.com/modelcontextprotocol/typescript-sdk/blob/131776764536b5fdca642df51230a3746fb4ade0/src/server/mcp.ts#L705
43
+ // Since paramsSchema here is not undefined, the server will create a non-optional z.object from it.
44
+ const existingHandler = (
45
+ this . mcpServer . server [ "_requestHandlers" ] as Map <
46
+ string ,
47
+ ( request : unknown , extra : unknown ) => Promise < CallToolResult >
48
+ >
49
+ ) . get ( CallToolRequestSchema . shape . method . value ) ;
50
+
51
+ assert ( existingHandler , "No existing handler found for CallToolRequestSchema" ) ;
52
+
53
+ this . mcpServer . server . setRequestHandler ( CallToolRequestSchema , ( request , extra ) : Promise < CallToolResult > => {
54
+ if ( ! request . params . arguments ) {
55
+ request . params . arguments = { } ;
56
+ }
57
+
58
+ return existingHandler ( request , extra ) ;
59
+ } ) ;
60
+
36
61
await initializeLogger ( this . mcpServer , this . userConfig . logPath ) ;
37
62
38
63
await this . mcpServer . connect ( transport ) ;
0 commit comments