- 
                Notifications
    
You must be signed in to change notification settings  - Fork 174
 
Add Streamable Http Transport #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Streamable Http Transport #87
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @SeanChinJunKai, thanks for the PR. Please check the comments.
It also would be great to have some tests
        
          
                ...commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/StreamableHttpServerTransport.kt
          
            Show resolved
            Hide resolved
        
              
          
                ...commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/StreamableHttpServerTransport.kt
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | try { | ||
| val acceptHeader = call.request.headers["Accept"]?.split(",") ?: listOf() | ||
| 
               | 
          ||
| if (!acceptHeader.contains("text/event-stream") || !acceptHeader.contains("application/json")) { | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use match instead of contains
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@e5l do you mean regex match with call.request.headers["Accept"]? Why do you prefer match over contains?
        
          
                ...commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/StreamableHttpServerTransport.kt
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                ...commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/StreamableHttpServerTransport.kt
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | } | ||
| 
               | 
          ||
| if (sessionId != null) { | ||
| call.response.header("Mcp-Session-Id", sessionId!!) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constants should be extracted to the top-level scope with a proper visibility
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@e5l I have extracted it into a Constants file in shared module since we might need it in client module
| 
           Hi @e5l, thanks for the comments. Ill take a look. I have some questions regarding client transport implementation. In particular, when client makes post request to server server can open a sse session but how does client connect to this sse session?  | 
    
| 
           Hi @e5l, could i get some help on the above?  | 
    
| 
           Sure, let me check  | 
    
- Clear callMapping in close() method to prevent memory leaks - Add comprehensive KDoc comments for all public APIs - Fix Accept header validation to use proper matching instead of contains - Fix ContentType header key to use HttpHeaders.ContentType - Fix parseBody error message for invalid JSON format - Add unit tests for StreamableHttpServerTransport
- Clear callMapping in close() method to prevent memory leaks - Add comprehensive KDoc comments for all public APIs - Fix Accept header validation to use proper matching instead of contains - Fix ContentType header key to use HttpHeaders.ContentType - Fix parseBody error message for invalid JSON format - Add unit tests for StreamableHttpServerTransport
- Clear callMapping in close() method to prevent memory leaks - Add comprehensive KDoc comments for all public APIs - Fix Accept header validation to use proper matching instead of contains - Fix ContentType header key to use HttpHeaders.ContentType - Fix parseBody error message for invalid JSON format - Add unit tests for StreamableHttpServerTransport
- Clear callMapping in close() method to prevent memory leaks - Add comprehensive KDoc comments for all public APIs - Fix Accept header validation to use proper matching instead of contains - Fix ContentType header key to use HttpHeaders.ContentType - Fix parseBody error message for invalid JSON format - Add unit tests for StreamableHttpServerTransport
802d323    to
    257929a      
    Compare
  
    - add EventStore and support "Last-Event-ID" - Origin check - thread safety
d0e2593    to
    9613cd3      
    Compare
  
    There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces a new streamable HTTP transport for JSON-RPC over Server-Sent Events (SSE) and updates protocol message types.
- Bump protocol version and make JSONRPC IDs optional in response/error types
 - Add 
StreamableHttpServerTransportandEventStoreinterface for SSE-based streaming - Update API metadata to expose new transport and error constructors
 
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description | 
|---|---|
| src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/types.kt | Updated LATEST_PROTOCOL_VERSION, SUPPORTED_PROTOCOL_VERSIONS, and changed JSONRPCResponse/JSONRPCError signatures to accept nullable IDs | 
| src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/shared/Protocol.kt | Adjusted named parameter for JSONRPCError instantiation in fallback error response | 
| src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/StreamableHttpServerTransport.kt | Newly added HTTP transport implementation with SSE, session management, and optional resumability | 
| api/kotlin-sdk.api | Reflected changes to JSONRPCError, added EventStore interface, and StreamableHttpServerTransport to public API | 
Comments suppressed due to low confidence (3)
src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/types.kt:280
- Changing 
JSONRPCErrorfrom adata classto a regular class and adding an optionalidparameter is a breaking API change (removescopy, destructuring, andequals/hashCode). Document this in the changelog and provide migration guidance. 
public class JSONRPCError(
src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/StreamableHttpServerTransport.kt:92
- This new transport contains complex SSE and HTTP-session logic but no accompanying tests. Add unit and integration tests covering initialization, streaming, error handling, and session resumption.
 
public class StreamableHttpServerTransport(
src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/StreamableHttpServerTransport.kt:206
- [nitpick] Consider adding KDoc to 
handleRequest(and other public methods) explaining the expected HTTP headers and lifecycle to improve readability and maintainability. 
    public suspend fun handleRequest(call: ApplicationCall, session: ServerSSESession) {
| transport?.send( | ||
| JSONRPCResponse( | ||
| id = request.id, | ||
| error = JSONRPCError( | 
    
      
    
      Copilot
AI
    
    
    
      Jul 11, 2025 
    
  
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSONRPCError is instantiated without passing the original request.id, so clients cannot correlate the method-not-found error with the request. Include id = request.id when constructing JSONRPCError.
| error = JSONRPCError( | |
| error = JSONRPCError( | |
| id = request.id, | 
        
          
                ...commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/StreamableHttpServerTransport.kt
              
                Outdated
          
            Show resolved
            Hide resolved
        
      …r/StreamableHttpServerTransport.kt Co-authored-by: Copilot <[email protected]>
| 
           Error when testing with Inspector, needs further investigation There are also issues with the API design and limitations of ktor when creating the server  | 
    
| 
           Obsolete  | 
    
          
 Can we have some details on why this is obsolete? Is there ongoing work in any other PR?  | 
    
| 
           Hi @tidbeck  | 
    
| 
           Hi @devcrocod , I created a PR #235, hopefully we won't have duplicated work.  | 
    
Closes #79
Pending:
Motivation and Context
How Has This Been Tested?
Breaking Changes
Types of changes
Checklist
Additional context