Skip to content
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
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export default {
} catch {
return json(rpcError(null, -32700, "parse error"));
}
if (typeof body !== "object" || body === null || Array.isArray(body)) {
return json(rpcError(null, -32600, "Invalid Request"));
}

const result = await handle(body, env, caller);
// Notifications (no id) get a 202 with no body per JSON-RPC.
Expand Down
20 changes: 20 additions & 0 deletions test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ test("tools/call rejects non-object arguments", async (t) => {
assert.equal(upstreamCalls, 0);
});

test("non-object JSON-RPC bodies get -32600 Invalid Request", async (t) => {
const env = {
BRIDGEKIT_CLIENTS: JSON.stringify({
"client-key": { name: "test client", tools: [], allowWrite: false },
}),
};

for (const badBody of [null, [], "foo", 42, true]) {
await t.test(`body ${JSON.stringify(badBody)}`, async () => {
const response = await mcpRequest(env, badBody);

assert.deepEqual(await response.json(), {
jsonrpc: "2.0",
id: null,
error: { code: -32600, message: "Invalid Request" },
});
});
}
});

test("/ai rejects requests without a client key before calling the model", async () => {
let upstreamCalls = 0;
globalThis.fetch = async () => {
Expand Down