Skip to content

feat(transport): Add new transport modes (Streamable HTTP & SSE) #33

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

Merged
merged 6 commits into from
May 9, 2025

Conversation

eddyv
Copy link
Member

@eddyv eddyv commented May 9, 2025

This pull request introduces enhancements to the CLI, transport system, and overall server architecture, enabling support for multiple transport protocols (HTTP, SSE, and Stdio) and improving configurability and error handling. The changes also include updates to the logging level, environment schema, and tool handler interfaces.

CLI and Environment Configuration Enhancements:

  • Added support for specifying transport types via the CLI (--transport) with validation and parsing logic for multiple transport protocols (src/cli.ts).
  • Updated environment schema to include optional HTTP transport configuration (HTTP_PORT and HTTP_HOST) with default values.

Transport System Overhaul:

  • Introduced TransportManager to manage multiple transport protocols (HTTP, SSE, Stdio) with start/stop lifecycle management (src/mcp/transports/manager.ts).
  • Implemented HttpTransport to handle HTTP-based MCP sessions, including POST, GET, and DELETE endpoints for session management (src/mcp/transports/http.ts).
  • Updated index.ts to initialize and manage transports dynamically based on CLI options and environment variables.

Tool Handler Improvements:

  • Enhanced tool handlers to support an optional sessionId parameter for session-specific operations, improving flexibility for tools like Kafka message consumption.

Logging and Debugging:

  • Changed the default logging level from info to debug to provide more detailed runtime information.

Dependency and Build Updates:

  • Added @fastify/swagger, @fastify/swagger-ui, and fastify dependencies to support HTTP transport and API documentation (package.json).
  • Introduced new npm scripts (start:http, start:sse, start:all, help) to facilitate transport-specific server startups.

Testing

Tested using https://github.com/modelcontextprotocol/inspector (for streamable http & sse) and goose cli (sse). Not all clients support streamable http mode yet.

eddyv added 5 commits May 7, 2025 15:41
- Added support for HTTP transport in the MCP server, allowing communication via HTTP requests.
- Introduced a new `HttpTransport` class that handles incoming requests and manages sessions.
- Updated CLI options to include transport type selection, defaulting to stdio.
- Enhanced environment schema to configure HTTP host and port.
- Refactored the main server logic to utilize the selected transport type, improving flexibility in deployment.
- Added a base transport class to standardize transport implementations.
- Introduced a `TransportManager` class to manage multiple transport types (HTTP, SSE, STDIO) for the MCP server.
- Updated CLI options to allow selection of multiple transport types, improving flexibility in deployment.
- Refactored the `HttpTransport` and added a new `SseTransport` class for handling Server-Sent Events.
- Enhanced environment variable loading and error handling in the CLI.
- Removed the obsolete `BaseTransport` class and factory pattern, simplifying transport management.
- Updated package dependencies to include `@fastify/swagger` and `@fastify/swagger-ui` for API documentation.
- Improved logging configuration to default to debug level for better traceability.
…ection management

- Updated the HttpServer class to include a custom logger instance for improved logging capabilities.
- Configured the server to force close ongoing connections, enhancing resource management during shutdowns.
- Refactored the server initialization to accommodate the new logging setup.
@Copilot Copilot AI review requested due to automatic review settings May 9, 2025 17:03
@eddyv eddyv requested a review from a team as a code owner May 9, 2025 17:03
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 enhances the MCP Server by introducing support for multiple transport modes (HTTP, SSE, and STDIO), along with corresponding CLI, environment, and logging improvements. Key changes include:

  • Addition of new transport types and implementations for HTTP (streamable), SSE, and STDIO.
  • Introduction of a TransportManager to dynamically initialize and manage these transports.
  • Updates to CLI options, environment schemas, and logging configuration.

Reviewed Changes

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

Show a summary per file
File Description
src/mcp/transports/types.ts Introduces transport types and shared interfaces for the transports.
src/mcp/transports/stdio.ts Implements the STDIO transport.
src/mcp/transports/sse.ts Implements the SSE transport endpoints and session management.
src/mcp/transports/http.ts Implements the streamable HTTP transport with session handling.
src/mcp/transports/server.ts Sets up an HTTP server with Swagger documentation.
src/mcp/transports/manager.ts Provides lifecycle management for starting/stopping transports.
src/cli.ts Updates CLI parsing to accept comma-separated transport types.
src/env-schema.ts Adds HTTP transport configuration to the environment schema.
src/logger.ts Changes default logging level from info to debug.
src/index.ts Initializes the TransportManager and updates tool handler invocations.
package.json Adds npm scripts for starting the server with different transport modes.

Comment on lines +141 to +142
Object.values(this.sessions).forEach((transport) => {
const sessionId = transport.sessionId;
Copy link
Preview

Copilot AI May 9, 2025

Choose a reason for hiding this comment

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

The 'this.sessions' variable is a Map, so using Object.values() may not iterate over the stored transports as expected. Consider using 'this.sessions.forEach((transport, sessionId) => { ... })' or iterating over 'this.sessions.values()' for correct behavior.

Suggested change
Object.values(this.sessions).forEach((transport) => {
const sessionId = transport.sessionId;
this.sessions.forEach((transport, sessionId) => {

Copilot uses AI. Check for mistakes.

Copy link
Member Author

@eddyv eddyv May 9, 2025

Choose a reason for hiding this comment

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

Modifications of this file were necessary to help remove typing errors now that we've introduced a number type to our env-schema.

Prevents issues when accessing env.* when our client args would expect a string type and not a string | number

Comment on lines +17 to +18
// Useful for when ongoing http/sse connections are present
forceCloseConnections: true,
Copy link
Member Author

Choose a reason for hiding this comment

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

Still not sure if this is better or if it's better for us to modify keep alive time

@eddyv eddyv merged commit 01b8676 into main May 9, 2025
1 of 2 checks passed
@eddyv eddyv deleted the feature/streamable-http branch May 9, 2025 20:53
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.

2 participants