Skip to content

fix: workaround models not providing arguments when everything is optional #116

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

Merged
merged 3 commits into from
Apr 24, 2025
Merged
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
25 changes: 25 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { mongoLogId } from "mongodb-log-writer";
import { ObjectId } from "mongodb";
import { Telemetry } from "./telemetry/telemetry.js";
import { UserConfig } from "./config.js";
import { CallToolRequestSchema, CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import assert from "assert";

export interface ServerOptions {
session: Session;
Expand All @@ -33,6 +35,29 @@ export class Server {
this.registerTools();
this.registerResources();

// This is a workaround for an issue we've seen with some models, where they'll see that everything in the `arguments`
// object is optional, and then not pass it at all. However, the MCP server expects the `arguments` object to be if
// the tool accepts any arguments, even if they're all optional.
//
// see: https://github.com/modelcontextprotocol/typescript-sdk/blob/131776764536b5fdca642df51230a3746fb4ade0/src/server/mcp.ts#L705
// Since paramsSchema here is not undefined, the server will create a non-optional z.object from it.
const existingHandler = (
this.mcpServer.server["_requestHandlers"] as Map<
string,
(request: unknown, extra: unknown) => Promise<CallToolResult>
>
).get(CallToolRequestSchema.shape.method.value);

assert(existingHandler, "No existing handler found for CallToolRequestSchema");

this.mcpServer.server.setRequestHandler(CallToolRequestSchema, (request, extra): Promise<CallToolResult> => {
if (!request.params.arguments) {
request.params.arguments = {};
}

return existingHandler(request, extra);
});

await initializeLogger(this.mcpServer, this.userConfig.logPath);

await this.mcpServer.connect(transport);
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/tools/mongodb/metadata/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ describeWithMongoDB("Connect tool", (integration) => {
},
]);

describe("without arguments", () => {
it("prompts for connection string if not set", async () => {
const response = await integration.mcpClient().callTool({ name: "connect" });
const content = getResponseContent(response.content);
expect(content).toContain("No connection details provided");
});

it("connects to the database if connection string is set", async () => {
config.connectionString = integration.connectionString();

const response = await integration.mcpClient().callTool({ name: "connect" });
const content = getResponseContent(response.content);
expect(content).toContain("Successfully connected");
expect(content).toContain(integration.connectionString());
});
});

describe("with default config", () => {
describe("without connection string", () => {
it("prompts for connection string", async () => {
Expand Down
Loading