Commit beb7be5
Fix MCP server crash on cancellation and structured_content validation (#411)
* Fix MCP server crash on request cancellation
When a client cancels a long-running MCP request, there's a race condition
between the cancellation and normal response paths:
1. Client cancels request → RequestResponder.cancel() sends error response
and sets _completed = True
2. Middleware catches CancelledError and returns a ToolResult
3. MCP SDK tries to call message.respond(response)
4. Crash: assert not self._completed fails
Fix: Re-raise CancelledError instead of returning a result, allowing the
MCP SDK's cancellation handler to properly manage the response lifecycle.
See: modelcontextprotocol/python-sdk#1153
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Add structured_content to error responses for MCP SDK validation
When tools have an outputSchema (auto-generated from return type like
Dict[str, Any]), MCP SDK requires structured_content in all responses.
The middleware was returning ToolResult without structured_content for
error cases (timeout, exceptions), causing validation errors:
"Output validation error: outputSchema defined but no structured output returned"
Fix: Include structured_content with the same error data in all error responses.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Fix structured_content not populated for tools with return type annotations
FastMCP auto-generates outputSchema from return type annotations (e.g.,
-> Dict[str, Any]) but doesn't populate structured_content in ToolResult.
MCP SDK validation then fails: "outputSchema defined but no structured output"
Fix: Intercept successful results and populate structured_content from
JSON text content when missing. Only modifies results when:
1. structured_content is missing
2. There's exactly one TextContent item
3. The text is valid JSON that parses to a dict
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(mcp): apply async wrapper on all platforms to prevent cancellation crashes
The asyncio.to_thread() wrapper was only applied on Windows, but it's
needed on ALL platforms to enable proper cancellation handling.
Without this fix, when a sync tool runs longer than the client timeout:
1. Client sends cancellation
2. Sync tool blocks event loop, can't receive CancelledError
3. Tool eventually returns, but MCP SDK already responded to cancel
4. AssertionError: "Request already responded to" → server crashes
This was discovered when uploading 7,375 files triggered a timeout,
crashing the MCP server on macOS.
Extends the fix from PR #411 which added CancelledError handling in
middleware - that fix only works when cancellation can propagate,
which requires async execution via to_thread().
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Fix: don't set structured_content on error responses
Setting structured_content causes MCP SDK to validate it against the
tool's outputSchema. For error responses, the error dict {"error": True, ...}
doesn't match the expected return type (e.g., Union[str, List[Dict]]),
causing "Output validation error: 'result' is a required property".
Fix: Only set structured_content for successful responses, not errors.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
---------
Co-authored-by: Quentin Ambard <quentin.ambard@databricks.com>
Co-authored-by: Claude <noreply@anthropic.com>1 parent edc4f67 commit beb7be5
2 files changed
Lines changed: 72 additions & 63 deletions
Lines changed: 52 additions & 54 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
48 | 68 | | |
49 | 69 | | |
50 | 70 | | |
| |||
53 | 73 | | |
54 | 74 | | |
55 | 75 | | |
| 76 | + | |
| 77 | + | |
56 | 78 | | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
75 | 90 | | |
76 | 91 | | |
77 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
78 | 98 | | |
79 | | - | |
| 99 | + | |
80 | 100 | | |
81 | 101 | | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
| 102 | + | |
97 | 103 | | |
98 | 104 | | |
99 | 105 | | |
| |||
104 | 110 | | |
105 | 111 | | |
106 | 112 | | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
111 | 116 | | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
125 | 123 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
76 | | - | |
| 75 | + | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | | - | |
80 | | - | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
81 | 88 | | |
82 | 89 | | |
83 | | - | |
| 90 | + | |
| 91 | + | |
84 | 92 | | |
85 | 93 | | |
86 | 94 | | |
| |||
132 | 140 | | |
133 | 141 | | |
134 | 142 | | |
135 | | - | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
136 | 148 | | |
137 | | - | |
138 | | - | |
139 | | - | |
| 149 | + | |
| 150 | + | |
140 | 151 | | |
141 | 152 | | |
142 | 153 | | |
| |||
0 commit comments