Skip to content

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

SeanChinJunKai
Copy link
Contributor

@SeanChinJunKai SeanChinJunKai commented Apr 27, 2025

Closes #79

Pending:

  • Client Implementation of StreamableHttp
  • Unit Tests
  • Integration Tests

Motivation and Context

How Has This Been Tested?

Breaking Changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

@e5l e5l requested review from Copilot and e5l April 29, 2025 12:10
Copy link

@Copilot Copilot AI left a 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 streamable HTTP transport to handle JSON-RPC over SSE, enabling stateful streaming of responses. Key changes include:

  • Changing JSONRPCResponse.id to be nullable.
  • Adding a new transport implementation, StreamableHttpServerTransport, to manage SSE connections and handle HTTP POST, GET, and DELETE requests.
  • Incorporating error handling for invalid headers, media types, and session validation.

Reviewed Changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/types.kt Updated JSONRPCResponse to allow a nullable id, supporting error responses.
src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/StreamableHttpServerTransport.kt Added implementation for streamable HTTP server transport with methods for handling POST, GET, and DELETE requests over SSE.
Files not reviewed (1)
  • api/kotlin-sdk.api: Language not supported
Comments suppressed due to low confidence (1)

src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/StreamableHttpServerTransport.kt:74

  • [nitpick] Consider using the standard 'Content-Type' header key instead of ContentType.toString() to ensure the header name is correctly set.
correspondingCall.response.headers.append(ContentType.toString(), ContentType.Application.Json.toString())

Comment on lines +106 to +107
// TODO Check if we need to clear the callMapping or if call timeout after awhile
_onClose.invoke()
Copy link
Preview

Copilot AI Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review whether callMapping should be cleared in the close() method to free resources and avoid potential memory leaks.

Suggested change
// TODO Check if we need to clear the callMapping or if call timeout after awhile
_onClose.invoke()
callMapping.clear() // Clear callMapping to free resources and avoid potential memory leaks

Copilot uses AI. Check for mistakes.

Copy link
Contributor

@e5l e5l left a 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

try {
val acceptHeader = call.request.headers["Accept"]?.split(",") ?: listOf()

if (!acceptHeader.contains("text/event-stream") || !acceptHeader.contains("application/json")) {
Copy link
Contributor

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

Copy link
Contributor Author

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?

}

if (sessionId != null) {
call.response.header("Mcp-Session-Id", sessionId!!)
Copy link
Contributor

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

Copy link
Contributor Author

@SeanChinJunKai SeanChinJunKai May 3, 2025

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

@SeanChinJunKai
Copy link
Contributor Author

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?

@SeanChinJunKai
Copy link
Contributor Author

Hi @e5l, could i get some help on the above?

@e5l
Copy link
Contributor

e5l commented May 7, 2025

Sure, let me check

@e5l e5l self-assigned this May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding Streaming HTTP Transport
2 participants