Skip to content

Commit 0b808c9

Browse files
nickytonlineclaude
andcommitted
fix(mcp): use JSON-RPC 2.0 format for error responses
- Replace custom error format with standard JSON-RPC 2.0 structure - Use appropriate error codes: -32600 (Invalid Request), -32000 (Connection Closed), -32603 (Internal Error) - Improves MCP specification compliance 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 82d4ba5 commit 0b808c9

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/index.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,28 @@ const mcpHandler = async (req: express.Request, res: express.Response) => {
8484
logger.warn(
8585
"POST request without session ID for non-initialization request",
8686
);
87-
res
88-
.status(400)
89-
.json({ error: "Session ID required for non-initialization requests" });
87+
res.status(400).json({
88+
jsonrpc: "2.0",
89+
id: null,
90+
error: {
91+
code: -32600,
92+
message: "Session ID required for non-initialization requests"
93+
}
94+
});
9095
return;
9196
}
9297

9398
// Handle unknown session
9499
if (sessionId && !transports[sessionId]) {
95100
logger.warn("Request for unknown session", { sessionId });
96-
res.status(404).json({ error: "Session not found" });
101+
res.status(404).json({
102+
jsonrpc: "2.0",
103+
id: null,
104+
error: {
105+
code: -32000,
106+
message: "Session not found"
107+
}
108+
});
97109
return;
98110
}
99111

@@ -125,7 +137,14 @@ const mcpHandler = async (req: express.Request, res: express.Response) => {
125137
logger.error("Error handling MCP request", {
126138
error: error instanceof Error ? error.message : error,
127139
});
128-
res.status(500).json({ error: "Internal server error" });
140+
res.status(500).json({
141+
jsonrpc: "2.0",
142+
id: null,
143+
error: {
144+
code: -32603,
145+
message: "Internal server error"
146+
}
147+
});
129148
}
130149
};
131150

0 commit comments

Comments
 (0)