Skip to content

Conversation

@CNSeniorious000
Copy link

Move from sse_starlette import EventSourceResponse to where EventSourceResponse is used to defer importing uvicorn.

Motivation and Context

Fixes #1770

How Has This Been Tested?

import sys, mcp

assert "uvicorn" not in sys.modules

Breaking Changes

Types of changes

  • 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)
  • 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

Copilot AI review requested due to automatic review settings December 11, 2025 05:34
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes module import time by implementing lazy imports for sse_starlette.EventSourceResponse, which prevents the early loading of the uvicorn dependency when the mcp module is imported. The changes move the import statements from module level to function/method level, placing them right before EventSourceResponse is used.

Key Changes:

  • Removed top-level from sse_starlette import EventSourceResponse imports
  • Added function-level lazy imports in four locations where EventSourceResponse is used
  • Maintains consistent pattern across both sse.py and streamable_http.py

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/mcp/server/sse.py Moved EventSourceResponse import inside response_wrapper function within connect_sse method
src/mcp/server/streamable_http.py Added three lazy imports for EventSourceResponse in _handle_post_request, _handle_get_request, and _replay_events methods

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@maxisbey maxisbey closed this Dec 11, 2025
@CNSeniorious000 CNSeniorious000 deleted the lazy-import-sse-starlette branch December 11, 2025 18:09
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.

uvicorn got eagerly imported via sse-starlette

2 participants