Skip to content

Conversation

ranjitodedra
Copy link

Fixes #1484

Fixes the RuntimeError that occurs when mounting StreamableHTTP servers to existing ASGI applications. The error "Task group is not initialized. Make sure to use run()" was caused by missing session manager initialization.

Motivation and Context

RuntimeError: Task group is not initialized. Make sure to use run(). when trying to mount StreamableHTTP servers to existing ASGI applications (like Starlette). This was caused by missing session manager initialization. The documentation examples were incomplete and didn't show the required lifespan parameter needed to properly initialize the session manager.

How Has This Been Tested?

  • ✅ Tested all 4 mounting examples individually
  • ✅ Verified HTTP 200 responses instead of RuntimeError
  • ✅ Tested with PowerShell Invoke-WebRequest on Windows

Breaking Changes

No breaking changes. This is a documentation fix that provides the correct implementation pattern.

Types of changes

  • [ x] 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)
  • [ x] Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

N/A

Fixes modelcontextprotocol#1484
- Add contextlib import and lifespan function to all mounting examples
- Add stateless_http=True parameter to FastMCP constructors
- Add lifespan=lifespan parameter to Starlette apps
- Update README.md documentation with correct examples
- Fixes RuntimeError: Task group is not initialized. Make sure to use run()

Resolves the issue where mounting StreamableHTTP servers to existing ASGI
applications would fail with RuntimeError due to missing session manager
initialization.
Run scripts/update_readme_snippets.py to sync documentation with code changes.
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.

Documentation on mounting to existing ASGI server is missing information

1 participant