Skip to content

Commit 2ed562e

Browse files
committed
Apply Copilot suggestions
1 parent a8e0831 commit 2ed562e

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/mcp/server/fastmcp/server.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,25 @@ def _get_invocation_mode(self, info: Tool, client_supports_async: bool) -> Liter
354354
return None # Old clients don't see invocationMode field
355355

356356
# New clients see the invocationMode field
357-
if "async" in info.invocation_modes and len(info.invocation_modes) == 1:
357+
modes = info.invocation_modes
358+
if self._is_async_only(modes):
358359
return "async" # Async-only
359-
elif len(info.invocation_modes) > 1 or info.invocation_modes == ["sync"]:
360+
if self._is_sync_only(modes) or self._is_hybrid(modes):
360361
return "sync" # Hybrid or explicit sync
361362
return None
362363

364+
def _is_async_only(self, modes: list[InvocationMode]) -> bool:
365+
"""Return True if invocation_modes is async-only."""
366+
return modes == ["async"]
367+
368+
def _is_sync_only(self, modes: list[InvocationMode]) -> bool:
369+
"""Return True if invocation_modes is sync-only."""
370+
return modes == ["sync"]
371+
372+
def _is_hybrid(self, modes: list[InvocationMode]) -> bool:
373+
"""Return True if invocation_modes contains both sync and async."""
374+
return "sync" in modes and "async" in modes and len(modes) > 1
375+
363376
async def list_tools(self) -> list[MCPTool]:
364377
"""List all available tools."""
365378
tools = self._tool_manager.list_tools()

src/mcp/server/lowlevel/server.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,15 @@ async def handler(req: types.CallToolRequest):
535535
)
536536
logger.debug(f"Created async operation with token: {operation.token}")
537537

538-
ctx = self.request_context
538+
# Add the operation token to the request context
539+
ctx = RequestContext(
540+
request_id=self.request_context.request_id,
541+
operation_token=self.request_context.operation_token,
542+
meta=self.request_context.meta,
543+
session=self.request_context.session,
544+
lifespan_context=self.request_context.lifespan_context,
545+
request=self.request_context.request,
546+
)
539547
ctx.operation_token = operation.token
540548
request_ctx.set(ctx)
541549

src/mcp/server/streamable_http.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ def _is_async_operation_response(self, response_message: JSONRPCMessage) -> bool
325325
return bool(operation["token"]) # type: ignore
326326

327327
return False
328-
except (TypeError, KeyError, AttributeError):
328+
except (TypeError, KeyError, AttributeError) as exc:
329+
logger.exception("Exception in _is_async_operation_response: %s", exc)
329330
return False
330331

331332
async def _handle_sse_mode(

0 commit comments

Comments
 (0)