Skip to content

FastMCP's +Starlette's Router => 307 Temporary Redirect for /mcp #1168

@bluedog13

Description

@bluedog13

Question

Issue Description

When using FastMCP's streamable_http_app(), requests to the /mcp endpoint result in a 307 Temporary Redirect to /mcp/, causing unnecessary latency and potential client compatibility issues.

Current Behavior

INFO:     127.0.0.1:58306 - "POST /mcp HTTP/1.1" 307 Temporary Redirect
INFO:     127.0.0.1:58306 - "POST /mcp/ HTTP/1.1" 200 OK

## Code

```python
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("My MCP Server")
app = mcp.streamable_http_app()

Root Cause

FastMCP uses Starlette's Router with redirect_slashes=True by default, which automatically redirects URLs without trailing slashes to URLs with trailing slashes using HTTP 307.

K8s Deployment Issues

In Kubernetes environments, this redirect behavior causes additional problems:

  1. Ingress Controllers: Some ingress controllers (nginx, traefik) may not preserve request bodies during 307 redirects
  2. Load Balancers: External load balancers may timeout or fail on redirect chains
  3. Service Mesh: Istio/Linkerd may not handle redirects properly for streaming connections
  4. TLS Termination: HTTPS redirects between services can cause certificate validation issues

Impact

  1. Performance: Extra round-trip for every MCP request
  2. Client Compatibility: Some HTTP clients may not handle 307 redirects properly
  3. Debugging Confusion: Logs show double requests for every operation

Attempted Solutions

Solution 1: Disable redirect_slashes (Breaks Routing)

app = mcp.streamable_http_app()
app.router.redirect_slashes = False  # This breaks MCP routing entirely

Result: 404 Not Found for all MCP requests

Solution 2: Manual Route Configuration (Complex)

Requires understanding FastMCP's internal routing structure.

Proposed Solutions

Option 1: Add Configuration Parameter

app = mcp.streamable_http_app(redirect_slashes=False)

Option 2: Canonical Endpoint Documentation

Clearly document whether the canonical endpoint is /mcp or /mcp/ and ensure consistency.

Option 3: Handle Both Routes Explicitly

FastMCP could register both /mcp and /mcp/ routes to avoid redirects.

Environment

  • mcp version:1.11.0
  • Python version: 3.11.3
  • HTTP Transport: SSE (Server-Sent Events)
  • Framework: Starlette (via FastMCP)

Reproduction Steps

  1. Create FastMCP server with streamable_http_app()
  2. Make POST request to /mcp (without trailing slash)
  3. Observe 307 redirect in logs
  4. Request is automatically redirected to /mcp/

Request

Could FastMCP provide a way to either:

  1. Disable the redirect behavior without breaking routing
  2. Configure the canonical endpoint path
  3. Handle both /mcp and /mcp/ without redirects

This would improve performance and reduce confusion for developers implementing MCP servers.

Additional Context

Environment

  • mcp version:1.11.0
  • Python version: 3.11.3
  • HTTP Transport: SSE (Server-Sent Events)
  • Framework: Starlette (via FastMCP)

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions