Skip to content

Commit 0375535

Browse files
committed
mcp: implement support for JSON responses in the MCP streamable server
Add a new (currently unexported) jsonResponse option to StreamableServerTransportOptions, and use it to control the response content type, serving application/json if set. Additionally: - A transportOptions field is added to the StreamableHTTPOptions. - A messages iterator is added to encapsulate the iteration of stream messages, since the handling of JSON and SSE responses are otherwise very different. - The serving flow is refactored to avoid returning (statusCode, message), primarily because this seemed liable to lead to redundant calls to WriteHeader, because only local logic knows whether or not any data has been written to the response. - The serving flow is refactored to delegate to responseJSON and responseSSE, according to the currently unexported jsonResponse option. - A bug is fixed where all GET streams were considered persistent: the terminal condition req.Method == http.MethodPost && nOutstanding == 0 was not right: GET requests may implement stream resumption. Updates #211
1 parent 6ea7a6c commit 0375535

File tree

4 files changed

+315
-179
lines changed

4 files changed

+315
-179
lines changed

mcp/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (c *Client) listRoots(_ context.Context, _ *ClientSession, _ *ListRootsPara
250250
func (c *Client) createMessage(ctx context.Context, cs *ClientSession, params *CreateMessageParams) (*CreateMessageResult, error) {
251251
if c.opts.CreateMessageHandler == nil {
252252
// TODO: wrap or annotate this error? Pick a standard code?
253-
return nil, &jsonrpc2.WireError{Code: CodeUnsupportedMethod, Message: "client does not support CreateMessage"}
253+
return nil, jsonrpc2.NewError(CodeUnsupportedMethod, "client does not support CreateMessage")
254254
}
255255
return c.opts.CreateMessageHandler(ctx, cs, params)
256256
}

mcp/shared.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ func sessionMethod[S Session, P Params, R Result](f func(S, context.Context, P)
275275

276276
// Error codes
277277
const (
278+
// TODO: should these be unexported?
279+
278280
CodeResourceNotFound = -32002
279281
// The error code if the method exists and was called properly, but the peer does not support it.
280282
CodeUnsupportedMethod = -31001

0 commit comments

Comments
 (0)