Skip to content

feat: Add RawHttpServerAdapter for HTTP frameworks #474

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

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

Conversation

Njuelle
Copy link

@Njuelle Njuelle commented May 9, 2025

This PR introduces a RawHttpServerAdapter to facilitate the integration of the MCP SDK's StreamableHTTPServerTransport with Node.js web frameworks like Fastify and Express. It also includes tests for this new adapter, an example server using Fastify, and updates to the README.

Motivation and Context

This PR addresses my issue #441

While the SDK's StreamableHTTPServerTransport uses native Node.js HTTP objects (which is foundational), directly integrating it with frameworks that have their own request/response abstractions can be verbose. The new RawHttpServerAdapter simplifies this by providing a reusable Transport implementation. It's designed for frameworks that expose the raw Node.js request/response objects (e.g., via request.raw), making it significantly easier to use the MCP SDK with popular choices like Fastify. This improves versatility, promotes wider adoption, and streamlines HTTP-based MCP server setup.

How Has This Been Tested?

  • Unit tests for RawHttpServerAdapter have been added (src/server/raw-http-adapter.test.ts), mocking the underlying StreamableHTTPServerTransport to verify correct interaction and lifecycle management.
  • A new example server using Fastify (src/examples/server/simpleFastifyServer.ts) has been created and manually tested to ensure it:
    • Initializes correctly with the RawHttpServerAdapter.
    • Exposes tools, resources, and prompts as defined.
    • Handles MCP initialize, tools/call, readResource, and prompts/get requests correctly over HTTP.
    • Supports Server-Sent Events (SSE) for streaming responses and notifications.
    • Handles session management via the underlying transport.

Breaking Changes

None. This change is additive and does not alter existing interfaces or functionality in a breaking way.

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 (based on the unit tests added)
  • I have added appropriate error handling (within the example and adapter for standard HTTP errors)
  • I have added or updated documentation as needed (README updated with the new adapter)

Additional context

  • The RawHttpServerAdapter is designed to be generic for any Node.js framework that exposes the raw IncomingMessage and ServerResponse objects (e.g., request.raw, reply.raw in Fastify; req, res in Express).
  • The Fastify example (simpleFastifyServer.ts) now mirrors the tools, resources, and prompts from the simpleStreamableHttp.ts example to provide a comparable demonstration of functionality using a different web server setup.
  • The decision to name the adapter RawHttpServerAdapter (and the file raw-http-adapter.ts) was made to clearly indicate its reliance on raw Node.js HTTP objects.

Fixes #441

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.

Fastify support or custom HTTP adapter?
1 participant