Skip to content

Commit 1637b70

Browse files
committed
feat: add support for disabling output validation for tools
1 parent 6a84a2f commit 1637b70

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/mcp/client/session.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ async def call_tool(
287287
arguments: dict[str, Any] | None = None,
288288
read_timeout_seconds: timedelta | None = None,
289289
progress_callback: ProgressFnT | None = None,
290+
validate_output: bool = True,
290291
) -> types.CallToolResult:
291292
"""Send a tools/call request with optional progress callback support."""
292293

@@ -305,7 +306,7 @@ async def call_tool(
305306
progress_callback=progress_callback,
306307
)
307308

308-
if not result.isError:
309+
if not result.isError and validate_output:
309310
await self._validate_tool_result(name, result)
310311

311312
return result

src/mcp/server/lowlevel/server.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,12 @@ async def _get_cached_tool_definition(self, tool_name: str) -> types.Tool | None
423423

424424
return tool
425425

426-
def call_tool(self, *, validate_input: bool = True):
426+
def call_tool(self, *, validate_input: bool = True, validate_output: bool = True):
427427
"""Register a tool call handler.
428428
429429
Args:
430430
validate_input: If True, validates input against inputSchema. Default is True.
431+
validate_output: If True, validates output against outputSchema. Default is True.
431432
432433
The handler validates input against inputSchema (if validate_input=True), calls the tool function,
433434
and builds a CallToolResult with the results:
@@ -486,10 +487,11 @@ async def handler(req: types.CallToolRequest):
486487
"Output validation error: outputSchema defined but no structured output returned"
487488
)
488489
else:
489-
try:
490-
jsonschema.validate(instance=maybe_structured_content, schema=tool.outputSchema)
491-
except jsonschema.ValidationError as e:
492-
return self._make_error_result(f"Output validation error: {e.message}")
490+
if validate_output:
491+
try:
492+
jsonschema.validate(instance=maybe_structured_content, schema=tool.outputSchema)
493+
except jsonschema.ValidationError as e:
494+
return self._make_error_result(f"Output validation error: {e.message}")
493495

494496
# result
495497
return types.ServerResult(

0 commit comments

Comments
 (0)