|
| 1 | +# Bug: MCP server 307 redirect breaks Claude CLI integration |
| 2 | + |
| 3 | +## Bug Description |
| 4 | +When users try to add the pyplots MCP server using the Claude CLI command: |
| 5 | +```bash |
| 6 | +claude mcp add pyplots --transport http https://api.pyplots.ai/mcp |
| 7 | +``` |
| 8 | + |
| 9 | +The server is added successfully, but when Claude CLI tries to connect, it fails with "Failed to connect" status. The server shows as configured but non-functional. |
| 10 | + |
| 11 | +**Expected behavior**: Claude CLI should successfully connect to the MCP server and list available tools. |
| 12 | + |
| 13 | +**Actual behavior**: Connection fails silently. Running `claude mcp get pyplots` shows `Status: ✗ Failed to connect`. |
| 14 | + |
| 15 | +## Problem Statement |
| 16 | +The pyplots MCP endpoint at `https://api.pyplots.ai/mcp` returns a **307 Temporary Redirect** to `http://api.pyplots.ai/mcp/` when accessed without a trailing slash. This causes two issues: |
| 17 | + |
| 18 | +1. **Claude CLI doesn't follow HTTP redirects** - MCP clients generally do not follow 307 redirects |
| 19 | +2. **HTTPS-to-HTTP downgrade** - The redirect location uses `http://` instead of `https://`, which is a security concern |
| 20 | + |
| 21 | +## Solution Statement |
| 22 | +Update the documentation to instruct users to use the URL with a trailing slash: `https://api.pyplots.ai/mcp/`. This avoids the redirect issue entirely since the server responds correctly when the trailing slash is included. |
| 23 | + |
| 24 | +This is the minimal fix that resolves the issue without requiring code changes to the MCP server or FastAPI configuration. |
| 25 | + |
| 26 | +## Steps to Reproduce |
| 27 | +1. Add the MCP server using Claude CLI: |
| 28 | + ```bash |
| 29 | + claude mcp add pyplots --transport http https://api.pyplots.ai/mcp |
| 30 | + ``` |
| 31 | +2. Check the server status: |
| 32 | + ```bash |
| 33 | + claude mcp get pyplots |
| 34 | + ``` |
| 35 | +3. Observe: `Status: ✗ Failed to connect` |
| 36 | + |
| 37 | +**Verification that trailing slash works:** |
| 38 | +```bash |
| 39 | +# Remove and re-add with trailing slash |
| 40 | +claude mcp remove pyplots |
| 41 | +claude mcp add pyplots --transport http https://api.pyplots.ai/mcp/ |
| 42 | +claude mcp get pyplots |
| 43 | +# Observe: Status should show connected with tools listed |
| 44 | +``` |
| 45 | + |
| 46 | +## Root Cause Analysis |
| 47 | +The root cause is a combination of factors: |
| 48 | + |
| 49 | +1. **Starlette/FastAPI redirect behavior**: When an app is mounted at a path, Starlette's router has `redirect_slashes=True` by default. This means requests to `/mcp` are redirected to `/mcp/` with a 307 status code. |
| 50 | + |
| 51 | +2. **MCP client behavior**: Claude CLI (and other MCP clients) do not follow HTTP redirects per the MCP protocol specification. They expect the endpoint to respond directly without redirects. |
| 52 | + |
| 53 | +3. **HTTP downgrade in redirect**: The 307 redirect location header contains `http://` instead of `https://`, likely due to how the request is proxied through Google Cloud Run. |
| 54 | + |
| 55 | +**Technical verification:** |
| 56 | +```bash |
| 57 | +# Without trailing slash - returns 307 redirect |
| 58 | +curl -s -D - https://api.pyplots.ai/mcp 2>&1 | head -5 |
| 59 | +# HTTP/2 307 |
| 60 | +# location: http://api.pyplots.ai/mcp/ |
| 61 | + |
| 62 | +# With trailing slash - works correctly |
| 63 | +curl -s -H "Accept: application/json, text/event-stream" -H "Content-Type: application/json" \ |
| 64 | + -X POST -d '{"jsonrpc":"2.0","method":"initialize",...}' https://api.pyplots.ai/mcp/ |
| 65 | +# Returns 200 with MCP response |
| 66 | +``` |
| 67 | + |
| 68 | +## Relevant Files |
| 69 | +Use these files to fix the bug: |
| 70 | + |
| 71 | +### Existing Files |
| 72 | +- `docs/reference/mcp.md` - MCP documentation that needs URL updates to include trailing slash |
| 73 | +- `app/src/pages/McpPage.tsx` - Frontend MCP page that shows configuration examples |
| 74 | + |
| 75 | +### New Files |
| 76 | +None required. |
| 77 | + |
| 78 | +## Step by Step Tasks |
| 79 | + |
| 80 | +### 1. Update MCP Documentation |
| 81 | +Update `docs/reference/mcp.md` to use trailing slash in all URL examples: |
| 82 | + |
| 83 | +- Change `https://api.pyplots.ai/mcp` to `https://api.pyplots.ai/mcp/` in: |
| 84 | + - Quick Start section (Claude Desktop config) |
| 85 | + - Claude Code (CLI) section |
| 86 | + - MCP Inspector command |
| 87 | + - Technical Details section |
| 88 | + - Troubleshooting section (curl command) |
| 89 | + - Any other occurrences |
| 90 | + |
| 91 | +### 2. Update Frontend MCP Page |
| 92 | +Update `app/src/pages/McpPage.tsx` to use trailing slash in configuration examples: |
| 93 | + |
| 94 | +- Update the JSON configuration examples shown to users |
| 95 | +- Ensure consistency with documentation |
| 96 | + |
| 97 | +### 3. Verify the Fix |
| 98 | +Manually test that the trailing slash URL works: |
| 99 | + |
| 100 | +```bash |
| 101 | +# Remove old config |
| 102 | +claude mcp remove pyplots |
| 103 | + |
| 104 | +# Add with trailing slash |
| 105 | +claude mcp add pyplots --transport http https://api.pyplots.ai/mcp/ |
| 106 | + |
| 107 | +# Verify connection works |
| 108 | +claude mcp get pyplots |
| 109 | +# Should show: Status: ✓ Connected (or similar success indicator) |
| 110 | +``` |
| 111 | + |
| 112 | +### 4. Run Validation Commands |
| 113 | +Execute all validation commands to ensure no regressions. |
| 114 | + |
| 115 | +## Validation Commands |
| 116 | +Execute every command to validate the bug is fixed with zero regressions. |
| 117 | + |
| 118 | +- `uv run ruff check docs/reference/mcp.md && uv run ruff format docs/reference/mcp.md` - Lint documentation (will pass, md file) |
| 119 | +- `cd app && yarn build` - Verify frontend builds without errors |
| 120 | +- `claude mcp remove pyplots 2>/dev/null; claude mcp add pyplots --transport http https://api.pyplots.ai/mcp/; claude mcp get pyplots` - Test the fix works |
| 121 | + |
| 122 | +## Final Check |
| 123 | +- Use `mcp__plugin_serena_serena__think_about_whether_you_are_done` to verify all tasks are complete. |
| 124 | + |
| 125 | +## Notes |
| 126 | +- **Alternative solutions considered but not chosen:** |
| 127 | + 1. **Disable redirect_slashes in FastAPI**: This would require code changes and may break other parts of the application |
| 128 | + 2. **Register both `/mcp` and `/mcp/` routes**: More complex and requires FastMCP changes |
| 129 | + 3. **Use a different mount pattern**: Would require significant refactoring |
| 130 | + |
| 131 | +- **Future consideration**: If FastMCP or the MCP protocol adds better redirect handling, the server-side fix could be revisited. For now, the documentation fix is the safest and quickest solution. |
| 132 | + |
| 133 | +- **Related issues**: |
| 134 | + - https://github.com/jlowin/fastmcp/issues/1544 |
| 135 | + - https://github.com/jlowin/fastmcp/issues/1364 |
| 136 | + - https://github.com/modelcontextprotocol/python-sdk/issues/1168 |
0 commit comments