Skip to content

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

2 participants