Skip to content

fix: use factory function for MCP server in stateless mode#71

Merged
xiaoyijun merged 1 commit intomasterfrom
xiaoyijun-fix-stateless-transport
Jan 24, 2026
Merged

fix: use factory function for MCP server in stateless mode#71
xiaoyijun merged 1 commit intomasterfrom
xiaoyijun-fix-stateless-transport

Conversation

@xiaoyijun
Copy link
Copy Markdown
Contributor

Summary

Fix the transport handling issue in the todo-manager tutorial code.

Problem: The original code used a singleton McpServer instance but called server.connect(transport) on every POST request. This is incorrect because in stateless mode, each request should have its own isolated server instance.

Solution: Following the MCP SDK's official simpleStatelessStreamableHttp.js example, use a factory function createMcpServer() to create a new server instance for each request.

Changes:

  • Wrap McpServer creation and tool registration in a createMcpServer() factory function
  • Call createMcpServer() in each POST request handler
  • Add mcpServer.close() cleanup in the response close handler
  • Remove unnecessary app.get('/') and app.delete('/') routes that only returned 405 errors

Testing

N/A

Checklist

  • .changeset
  • unit tests
  • integration tests
  • necessary TSDoc comments

@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying mcp-auth-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 93ca312
Status: ✅  Deploy successful!
Preview URL: https://e6a8f629.mcp-auth-docs.pages.dev
Branch Preview URL: https://xiaoyijun-fix-stateless-tran.mcp-auth-docs.pages.dev

View logs

@xiaoyijun xiaoyijun requested a review from a team January 24, 2026 00:59
@xiaoyijun xiaoyijun self-assigned this Jan 24, 2026
@xiaoyijun xiaoyijun merged commit b0de34e into master Jan 24, 2026
2 checks passed
@xiaoyijun xiaoyijun deleted the xiaoyijun-fix-stateless-transport branch January 24, 2026 04:59
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