feat: Add RawHttpServerAdapter for HTTP frameworks #474
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces a
RawHttpServerAdapter
to facilitate the integration of the MCP SDK'sStreamableHTTPServerTransport
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 newRawHttpServerAdapter
simplifies this by providing a reusableTransport
implementation. It's designed for frameworks that expose the raw Node.js request/response objects (e.g., viarequest.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?
RawHttpServerAdapter
have been added (src/server/raw-http-adapter.test.ts
), mocking the underlyingStreamableHTTPServerTransport
to verify correct interaction and lifecycle management.src/examples/server/simpleFastifyServer.ts
) has been created and manually tested to ensure it:RawHttpServerAdapter
.initialize
,tools/call
,readResource
, andprompts/get
requests correctly over HTTP.Breaking Changes
None. This change is additive and does not alter existing interfaces or functionality in a breaking way.
Types of changes
Checklist
Additional context
RawHttpServerAdapter
is designed to be generic for any Node.js framework that exposes the rawIncomingMessage
andServerResponse
objects (e.g.,request.raw
,reply.raw
in Fastify;req
,res
in Express).simpleFastifyServer.ts
) now mirrors the tools, resources, and prompts from thesimpleStreamableHttp.ts
example to provide a comparable demonstration of functionality using a different web server setup.RawHttpServerAdapter
(and the fileraw-http-adapter.ts
) was made to clearly indicate its reliance on raw Node.js HTTP objects.Fixes #441