René's URL Explorer Experiment


Title: MCP Server Session Lost in Multi-Worker Environment · Issue #520 · modelcontextprotocol/python-sdk · GitHub

Open Graph Title: MCP Server Session Lost in Multi-Worker Environment · Issue #520 · modelcontextprotocol/python-sdk

X Title: MCP Server Session Lost in Multi-Worker Environment · Issue #520 · modelcontextprotocol/python-sdk

Description: Describe the bug When deploying the MCP server in a Kubernetes environment with gunicorn's multi-process configuration, SSE connections disconnect after a period of time, resulting in subsequent messages receiving a 404 error "Could not ...

Open Graph Description: Describe the bug When deploying the MCP server in a Kubernetes environment with gunicorn's multi-process configuration, SSE connections disconnect after a period of time, resulting in subsequent me...

X Description: Describe the bug When deploying the MCP server in a Kubernetes environment with gunicorn's multi-process configuration, SSE connections disconnect after a period of time, resulting in subsequen...

Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/520

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"MCP Server Session Lost in Multi-Worker Environment","articleBody":"## Describe the bug\nWhen deploying the MCP server in a Kubernetes environment with gunicorn's multi-process configuration, SSE connections disconnect after a period of time, resulting in subsequent messages receiving a 404 error \"Could not find session for ID\". This occurs because SSE sessions are created in one worker process, but subsequent requests may be routed to different worker processes where the session state is not shared.\n\n## Steps to reproduce\n1. Deploy an MCP server in a Kubernetes environment using gunicorn\n2. Configure gunicorn with multiple workers (`workers \u003e 1`)\n3. Connect a client to the MCP server and establish an SSE connection (initial connection succeeds)\n4. Send the first message (successfully processed, returns 202)\n5. Wait a few seconds or try to send a second message\n6. Receive error: `WARNING mcp.server.sse Could not find session for ID: xxx-xxx-xxx`\n\n## Logs\n```\n[2025-04-15 19:26:26 +0800] [32] [INFO] connection open\n127.0.0.1:48270 - \"GET /mcps/sse HTTP/1.1\" 200\n127.0.0.1:48280 - \"POST /mcps/messages/?session_id=cb7ed84c8f2f4109b5712f9fb025a5c2 HTTP/1.1\" 202\n127.0.0.1:48280 - \"POST /mcps/messages/?session_id=cb7ed84c8f2f4109b5712f9fb025a5c2 HTTP/1.1\" 202\n127.0.0.1:48280 - \"POST /mcps/messages/?session_id=cb7ed84c8f2f4109b5712f9fb025a5c2 HTTP/1.1\" 202\n127.0.0.1:48280 - \"POST /mcps/messages/?session_id=cb7ed84c8f2f4109b5712f9fb025a5c2 HTTP/1.1\" 202\n127.0.0.1:48280 - \"POST /mcps/messages/?session_id=cb7ed84c8f2f4109b5712f9fb025a5c2 HTTP/1.1\" 202\n127.0.0.1:39380 - \"POST /mcps/messages/?session_id=cb7ed84c8f2f4109b5712f9fb025a5c2 HTTP/1.1\" 202\n[2025-04-15 19:26:47 +0800] [32] [WARNING] mcp.server.sse Could not find session for ID: cb7ed84c-8f2f-4109-b571-2f9fb025a5c2\n127.0.0.1:53124 - \"POST /mcps/messages/?session_id=cb7ed84c8f2f4109b5712f9fb025a5c2 HTTP/1.1\" 404\n[2025-04-15 19:26:47 +0800] [32] [ERROR] mcp.client.sse Error in post_writer: Client error '404 Not Found' for url 'http://localhost:8000/mcps/messages/?session_id=cb7ed84c8f2f4109b5712f9fb025a5c2'\n```\n\n## Expected behavior\nAll messages, whether initial or subsequent, should be processed normally without any session not found errors. Even when using multiple workers, SSE sessions should maintain continuous connections.\n\n## Environment information\n- Deployment environment: Kubernetes\n- Web server: gunicorn + uvicorn.workers.UvicornWorker\n- Session ID handling: Server converts UUIDs without hyphens to the hyphenated format\n- Local environment (running directly with uvicorn, single process) does not exhibit this issue\n\n## Reproduction conditions\n- Using gunicorn with multiple workers (`workers \u003e 1`)\n- Proxying multiple services (using ASGI routing)\n- Each worker maintains its own session state, with no session sharing between processes\n\n## Solution\nI resolved this issue by setting the worker count to 1:\n```bash\n# Before\nworkers=$(nproc --all)  # Use all available CPU cores\n\n# After\nworkers=1  # Use single worker to maintain session consistency\n```\n\nHowever, this is not an ideal solution as it limits the service's scalability.\n\n## Suggested improvements\n1. Implement distributed session storage (e.g., Redis) in the MCP server to allow multiple workers to share session state\n2. Document the MCP server's session management limitations in multi-process environments\n3. Provide session recovery/reconnection mechanisms to handle session disconnections\n\n## Additional context\n- Server code (`session.py`) shows that session state is stored in process memory\n- In our implementation, we solved the problem by separating the MCP service from the main application, deploying it independently with a single worker\n- Regular ping operations can mitigate the issue but cannot completely solve the session state sharing problem in multi-process environments\n\n## Potential solution approach\nCould the MCP server be modified to add a session storage abstraction layer that allows users to configure different session storage backends (memory, Redis, file, etc.) to support distributed deployments?","author":{"url":"https://github.com/Mile-Away","@type":"Person","name":"Mile-Away"},"datePublished":"2025-04-15T12:38:17.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":11},"url":"https://github.com/520/python-sdk/issues/520"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:1153543a-7284-d595-ef51-0fcc8b195836
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBE9A:1AEEB2:3E3EAD:55041E:6A5A6346
html-safe-noncebc62199998bf8dee5fff82d1e668349278a7ce96d9eb5e95f7f6cdeb0d663b60
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTlBOjFBRUVCMjozRTNFQUQ6NTUwNDFFOjZBNUE2MzQ2IiwidmlzaXRvcl9pZCI6IjM4NTcxMjg1NDMzOTk2MDA5NjYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac270fc688bf3a2c6c561ae61cc81234bf19a378ec10637e623ef2083b92d841b4
hovercard-subject-tagissue:2996289145
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/520/issue_layout
twitter:imagehttps://opengraph.githubassets.com/a96de4e858f5b9b7616d65f703cf2be266e65ec99400c3a75ebee4084e7199ad/modelcontextprotocol/python-sdk/issues/520
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/a96de4e858f5b9b7616d65f703cf2be266e65ec99400c3a75ebee4084e7199ad/modelcontextprotocol/python-sdk/issues/520
og:image:altDescribe the bug When deploying the MCP server in a Kubernetes environment with gunicorn's multi-process configuration, SSE connections disconnect after a period of time, resulting in subsequent me...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameMile-Away
hostnamegithub.com
expected-hostnamegithub.com
None05b9ddf6a47d2dbe13944873a99f5fb4b83ba4871f9cb8a8e256793a63ca9687
turbo-cache-controlno-preview
go-importgithub.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git
octolytics-dimension-user_id182288589
octolytics-dimension-user_loginmodelcontextprotocol
octolytics-dimension-repository_id862584018
octolytics-dimension-repository_nwomodelcontextprotocol/python-sdk
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id862584018
octolytics-dimension-repository_network_root_nwomodelcontextprotocol/python-sdk
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasef8d29d1bd03dda2dd14b3f80b8bc27e1111f43bd
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/modelcontextprotocol/python-sdk/issues/520#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F520
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F520
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=modelcontextprotocol%2Fpython-sdk
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/520
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/520
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/520
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/issues/520
modelcontextprotocol https://github.com/modelcontextprotocol
python-sdkhttps://github.com/modelcontextprotocol/python-sdk
Notifications https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Fork 3.7k https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Star 23.6k https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Code https://github.com/modelcontextprotocol/python-sdk
Issues 256 https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests 303 https://github.com/modelcontextprotocol/python-sdk/pulls
Actions https://github.com/modelcontextprotocol/python-sdk/actions
Projects https://github.com/modelcontextprotocol/python-sdk/projects
Models https://github.com/modelcontextprotocol/python-sdk/models
Security and quality 6 https://github.com/modelcontextprotocol/python-sdk/security
Insights https://github.com/modelcontextprotocol/python-sdk/pulse
Code https://github.com/modelcontextprotocol/python-sdk
Issues https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests https://github.com/modelcontextprotocol/python-sdk/pulls
Actions https://github.com/modelcontextprotocol/python-sdk/actions
Projects https://github.com/modelcontextprotocol/python-sdk/projects
Models https://github.com/modelcontextprotocol/python-sdk/models
Security and quality https://github.com/modelcontextprotocol/python-sdk/security
Insights https://github.com/modelcontextprotocol/python-sdk/pulse
davidroberts-merlyn/python-sdk#1https://github.com/davidroberts-merlyn/python-sdk/pull/1
MCP Server Session Lost in Multi-Worker Environmenthttps://github.com/modelcontextprotocol/python-sdk/issues/520#top
davidroberts-merlyn/python-sdk#1https://github.com/davidroberts-merlyn/python-sdk/pull/1
bugSomething isn't workinghttps://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22bug%22
needs confirmationNeeds confirmation that the PR is actually required or needed.https://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22needs%20confirmation%22
https://github.com/Mile-Away
Mile-Awayhttps://github.com/Mile-Away
on Apr 15, 2025https://github.com/modelcontextprotocol/python-sdk/issues/520#issue-2996289145
bugSomething isn't workinghttps://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22bug%22
needs confirmationNeeds confirmation that the PR is actually required or needed.https://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22needs%20confirmation%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.