Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use Mount instead of Route for SSE message handling #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

samihamine
Copy link

Description

This PR modifies how FastMCP handles SSE message endpoints by using Starlette's Mount instead of Route. This change follows the recommended approach from the MCP SDK team as discussed in modelcontextprotocol/python-sdk#83.

Problem

When using FastMCP with SSE transport, users encounter a TypeError: 'NoneType' object is not callable error. This occurs because:

  1. The current implementation uses a Starlette Route for handling SSE messages
  2. SseServerTransport.handle_post_message is designed as a full ASGI application
  3. Starlette's Route expects handlers to return a response object, but our handler doesn't

Solution

Following the guidance from @jspahrsummers in python-sdk#83, we:

  1. Replace Route("/messages", endpoint=handle_messages) with Mount("/messages/", app=sse.handle_post_message)
  2. Update the SSE transport path to include a trailing slash
  3. Remove the custom message handler since Mount handles the ASGI application directly

This approach:

  • Maintains compatibility with MCP SDK's ASGI application design
  • Properly handles the response flow
  • Follows the recommended pattern from the MCP SDK team

Changes

  • Modified run_sse_async() to use Mount instead of Route
  • Updated SSE transport initialization to use "/messages/" path
  • Added trailing slash to message endpoint path

Testing

  • Verified SSE connection works correctly
  • Confirmed message handling functions properly
  • Ensured no TypeError is raised during message handling

Related Issues

Fixes #69

This change follows the recommended approach from MCP SDK (modelcontextprotocol/python-sdk#83)
for handling SSE messages using Starlette's Mount instead of Route.

- Replace Route with Mount for /messages/ endpoint
- Update SseServerTransport path to include trailing slash
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.

SSE Mode: Error when connecting
1 participant