Skip to content

fix(mcp): make the tool-call log non-fatal when the MCP session is missing (#18391) - #18393

Closed
Jay0130-a wants to merge 1 commit into
PaddlePaddle:mainfrom
Jay0130-a:fix/mcp-tool-log-non-fatal
Closed

Jay0130-a wants to merge 1 commit into
PaddlePaddle:mainfrom
Jay0130-a:fix/mcp-tool-log-non-fatal

Conversation

@Jay0130-a

Copy link
Copy Markdown

What this PR does

Makes the per-call informational log in Task._invoke_tool best-effort, so a missing MCP request context or an unestablished MCP session can no longer fail a tool call.

mcp_server/paddleocr_mcp/tasks/base.py currently does:

ctx = get_context()
await ctx.info(
    f"--- {self.tool_name} tool received input_data: {normalized_input[:50]} ---"
)

before running inference. fastmcp.server.dependencies.get_context() raises when there is no active request context, and Context.info() raises while the session is not established:

ToolError: Error calling tool 'pp_structurev3': session is not available
because the MCP session has not been established yet.
Check `context.request_context` for None before accessing this attribute.

Because the server is created with mask_error_details=True, an MCP client only sees the generic Error calling tool 'pp_structurev3', with no indication that the log is what failed — the tool becomes uncallable and undiagnosable from the client side (reported in #18391; the PPStructureV3 pipeline itself works when called directly in the same venv).

Why it's needed

Logging is not part of the tool's contract. A transport/session that has not (yet) established a session must degrade to "no log line", not to a failed tool call. This is the guard the issue asks for, applied in the one place the context is actually touched, so it covers every task (ocr, pp_structurev3, paddleocr_vl, …) rather than a single tool.

How to verify

The guard has three branches; all three were exercised against the real module with a stubbed fastmcp.server.dependencies:

Scenario Before After
Healthy context tool runs, log emitted tool runs, log emitted
Context present, request_context is None RuntimeError → masked Error calling tool tool runs, no log
No active context (get_context() raises) RuntimeError tool runs, no log
OK  reproduced the reported ToolError class in the stub
OK  no_session      -> tool ran, no log, no exception
OK  no_context      -> tool ran, no log, no exception
OK  healthy context -> tool ran and logged: --- pp_structurev3 tool received input_data: inp ...

Risk & scope

Linked issues

Relates to #18391

…ssing

Task._invoke_tool() unconditionally called get_context() and awaited
ctx.info() before running inference. fastmcp raises when there is no active
request context, and ctx.info() raises while the MCP session has not been
established (request_context is None). Because the server is built with
mask_error_details=True, the client only saw a generic, undiagnosable
"Error calling tool 'pp_structurev3'" - the tool could not be invoked at all
in that environment (issue PaddlePaddle#18391).

Move the log into a best-effort helper that skips logging when the context or
the session is unavailable, so informational logging can never fail a tool
call. The inference path itself is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant