modelcontextprotocol / modelcontextprotocol/java-sdk
Support application/json responses in Streamable HTTP transport (opt-in JSON response mode)
还没有人认领这个 Issue。
- 主要语言
- Java
- 星标
- 3.7k
- 派生
- 1.1k
- 平均合并
- 1 天 15 小时
- 30 天内合并 PR
- 9
描述
Expected Behavior
An opt-in configuration to return application/json for JSON-RPC requests within the Streamable HTTP transport, even with active sessions. Example:
HttpServletStreamableServerTransportProvider.builder()
.jsonMapper(mapper)
.mcpEndpoint("/mcp")
.jsonResponse(true) // opt-in, default false
.build();
When enabled, doPost() for JSONRPCRequest would buffer the response from session.responseStream() and return it as application/json instead of opening an SSE stream. Server-initiated notifications would go through the standalone SSE stream (GET on the same /mcp endpoint, as defined in the Streamable HTTP spec).
All other official MCP SDKs already support this as an opt-in option:
| SDK | Config option | Default |
|---|---|---|
| Go | StreamableHTTPOptions.JSONResponse bool |
false |
| Python | is_json_response_enabled: bool |
False |
| TypeScript | enableJsonResponse?: boolean |
false |
| Rust | StreamableHttpServerConfig.json_response: bool |
false |
Current Behavior
In HttpServletStreamableServerTransportProvider.doPost(), session-bound JSONRPCRequest messages always get text/event-stream:
else if (message instanceof McpSchema.JSONRPCRequest jsonrpcRequest) {
// For streaming responses, we need to return SSE
response.setContentType(TEXT_EVENT_STREAM); // ← always SSE
// ...
}
There is no way to get application/json for session-bound requests. The initialize request (line 461) and HttpServletStatelessServerTransport (line 173) already return application/json - so the pattern exists in the codebase.
Context
The MCP Streamable HTTP spec (2025-06-18, §2.1.5) states:
"If the input is a JSON-RPC request, the server MUST either return
Content-Type: text/event-stream[...] orContent-Type: application/json, to return one JSON object."
This means session management does not require SSE for every response. There are scenarios where a server benefits from sessions (capability negotiation, logging, server-initiated notifications via GET) but its tool handlers produce simple single-message responses that don't need SSE framing.
The existing STATELESS mode (HttpServletStatelessServerTransport) does return application/json, but it removes session management entirely - no server-to-client notifications, no sampling, no capability tracking. This feature request is about supporting application/json responses within the Streamable transport, with sessions, as the spec allows and as the other SDKs already implement.
In local benchmarks comparing MCP server implementations across different SDKs, we observed that switching from SSE-only to JSON responses for simple tool calls (single request-response, no intermediate notifications) resulted in significant performance improvements, primarily due to eliminating chunked transfer encoding, AsyncContext lifecycle overhead, and SSE framing/parsing on both server and client sides.
Would it make sense to add this? Happy to contribute a PR if the maintainers agree.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 HttpServletStreamableServerTransportProvider.doPost() 开始,将其会话绑定的 JSON-RPC 路径与 initialize 路径和 HttpServletStatelessServerTransport 中对 application/json 的处理进行比较。完成的标准是:可选择启用的 jsonResponse 设置默认为 false,启用后为会话绑定的请求返回缓冲的 JSON,并保留用于服务器通知的独立 SSE 路径。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- java
- 领域
- api, backend
- Issue 类型
- 功能
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 冷清
- 描述清晰度
- 描述清楚
- 新手友好度
- 64/100