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

ranjitodedra and others added 3 commits October 18, 2025 01:01
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.
@felixweinberger felixweinberger added the documentation Improvements or additions to documentation label Oct 23, 2025
Copy link
Contributor

@felixweinberger felixweinberger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ranjitodedra thanks for this - looks like you also changed the examples to use stateless_http=True which is more than adding a lifespan param.

That's not covered in the description, was that change intentional? If so why was it changed?

@felixweinberger felixweinberger added the needs more work Not ready to be merged yet, needs additional changes. label Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation needs more work Not ready to be merged yet, needs additional changes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Documentation on mounting to existing ASGI server is missing information

2 participants