Skip to content

Commit 7824af8

Browse files
authored
fix(mcp): accept client-posted JSON-RPC responses on workflow MCP servers (#10508)
The Streamable HTTP transport requires a server to answer a POSTed JSON-RPC response with 202 and no body, as it already does for notifications. The workflow MCP serve route refused them with 400 -32600. It now accepts and ignores result and error responses, matching the SDK's streamable HTTP server; the route never sends server-to-client requests, so there is nothing to correlate them to. Remaining malformed-message 400s now log the body's shape (batch, not_an_object, invalid_version, invalid_response, missing_method, invalid_id) instead of a single not_a_request, without logging the body.
1 parent b1a1438 commit 7824af8

2 files changed

Lines changed: 87 additions & 3 deletions

File tree

‎apps/sim/app/api/mcp/serve/[serverId]/route.test.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,25 @@ describe('MCP Serve Route', () => {
828828
'a JSON-RPC 1.0 envelope',
829829
JSON.stringify({ jsonrpc: '1.0', id: 1, method: 'ping' }),
830830
ErrorCode.InvalidRequest,
831-
'not_a_request',
831+
'invalid_version',
832+
],
833+
[
834+
'a JSON-RPC batch',
835+
JSON.stringify([{ jsonrpc: '2.0', id: 1, method: 'ping' }]),
836+
ErrorCode.InvalidRequest,
837+
'batch',
838+
],
839+
[
840+
'a message without a method',
841+
JSON.stringify({ jsonrpc: '2.0', id: 1 }),
842+
ErrorCode.InvalidRequest,
843+
'missing_method',
844+
],
845+
[
846+
'a request with a null id',
847+
JSON.stringify({ jsonrpc: '2.0', id: null, method: 'ping' }),
848+
ErrorCode.InvalidRequest,
849+
'invalid_id',
832850
],
833851
])('names why it refuses %s on the route log line', async (_label, body, code, reason) => {
834852
dbChainMockFns.limit.mockResolvedValueOnce([
@@ -853,6 +871,35 @@ describe('MCP Serve Route', () => {
853871
expect(readResponseError(response)).toEqual({ errorCode: String(code), errorReason: reason })
854872
})
855873

874+
it.each([
875+
['result', { jsonrpc: '2.0', id: 'srv-1', result: {} }],
876+
['error', { jsonrpc: '2.0', id: 'srv-1', error: { code: -32601, message: 'Unsupported' } }],
877+
])(
878+
'accepts a client-posted JSON-RPC %s response with 202 and no body',
879+
async (_kind, message) => {
880+
dbChainMockFns.limit.mockResolvedValueOnce([
881+
{
882+
id: 'server-1',
883+
name: 'Public Server',
884+
workspaceId: 'ws-1',
885+
isPublic: true,
886+
createdBy: 'owner-1',
887+
},
888+
])
889+
890+
const req = new NextRequest('http://localhost:3000/api/mcp/serve/server-1', {
891+
method: 'POST',
892+
body: JSON.stringify(message),
893+
})
894+
895+
const response = await POST(req, createRouteContext({ serverId: 'server-1' }))
896+
897+
expect(response.status).toBe(202)
898+
expect(await response.text()).toBe('')
899+
expect(fetchMock).not.toHaveBeenCalled()
900+
}
901+
)
902+
856903
it('rejects streamed MCP request bodies that exceed the cap without content-length', async () => {
857904
const cancelSpy = vi.fn()
858905
dbChainMockFns.limit.mockResolvedValueOnce([

‎apps/sim/app/api/mcp/serve/[serverId]/route.ts‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import {
66
type CallToolResult,
77
ErrorCode,
88
type InitializeResult,
9+
isJSONRPCErrorResponse,
910
isJSONRPCNotification,
1011
isJSONRPCRequest,
12+
isJSONRPCResultResponse,
1113
type JSONRPCError,
1214
type JSONRPCMessage,
1315
type JSONRPCResultResponse,
1416
LATEST_PROTOCOL_VERSION,
1517
type ListToolsResult,
1618
type RequestId,
19+
RequestIdSchema,
1720
SUPPORTED_PROTOCOL_VERSIONS,
1821
type Tool,
1922
} from '@modelcontextprotocol/sdk/types.js'
@@ -152,11 +155,35 @@ function createError(
152155
}
153156
}
154157

158+
type NotARequestReason =
159+
| 'batch'
160+
| 'not_an_object'
161+
| 'invalid_response'
162+
| 'missing_method'
163+
| 'invalid_id'
164+
| 'invalid_version'
165+
| 'not_a_request'
166+
167+
/**
168+
* Names the shape of a body that is neither a notification, a response, nor a request, so the
169+
* log line says what a client sent without logging the body. JSON-RPC batches are refused: the
170+
* 2025-06-18 transport removed them and this route answers one message per POST.
171+
*/
172+
function describeNotARequest(body: unknown): NotARequestReason {
173+
if (Array.isArray(body)) return 'batch'
174+
if (!isRecordLike(body)) return 'not_an_object'
175+
if (body.jsonrpc !== '2.0') return 'invalid_version'
176+
if (body.method === undefined && ('result' in body || 'error' in body)) return 'invalid_response'
177+
if (typeof body.method !== 'string') return 'missing_method'
178+
if ('id' in body && !RequestIdSchema.safeParse(body.id).success) return 'invalid_id'
179+
return 'not_a_request'
180+
}
181+
155182
/** A 400 for a body that is not a usable JSON-RPC message, with its reason kept for the route's log line. */
156183
function malformedMessageResponse(
157184
code: ErrorCode,
158185
message: string,
159-
reason: 'invalid_json' | 'invalid_notification' | 'not_a_request' | 'invalid_request'
186+
reason: 'invalid_json' | 'invalid_notification' | 'invalid_request' | NotARequestReason
160187
): NextResponse {
161188
const response = NextResponse.json(createError(0, code, message), { status: 400 })
162189
describeResponseError(response, { errorCode: String(code), errorReason: reason })
@@ -781,11 +808,21 @@ export const POST = withRouteHandler(
781808
return new NextResponse(null, { status: 202 })
782809
}
783810

811+
const responseKind = isJSONRPCResultResponse(message)
812+
? 'result'
813+
: isJSONRPCErrorResponse(message)
814+
? 'error'
815+
: null
816+
if (responseKind) {
817+
logger.info('Ignored client JSON-RPC response', { kind: responseKind })
818+
return new NextResponse(null, { status: 202 })
819+
}
820+
784821
if (!isJSONRPCRequest(message)) {
785822
return malformedMessageResponse(
786823
ErrorCode.InvalidRequest,
787824
'Invalid JSON-RPC message',
788-
'not_a_request'
825+
describeNotARequest(body)
789826
)
790827
}
791828

0 commit comments

Comments
 (0)