Skip to content

feat: add support for disabling output validation for tools #1189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/mcp/client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ async def call_tool(
arguments: dict[str, Any] | None = None,
read_timeout_seconds: timedelta | None = None,
progress_callback: ProgressFnT | None = None,
validate_output: bool = True,
) -> types.CallToolResult:
"""Send a tools/call request with optional progress callback support."""

Expand All @@ -305,7 +306,7 @@ async def call_tool(
progress_callback=progress_callback,
)

if not result.isError:
if not result.isError and validate_output:
await self._validate_tool_result(name, result)

return result
Expand Down
12 changes: 7 additions & 5 deletions src/mcp/server/lowlevel/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,12 @@ async def _get_cached_tool_definition(self, tool_name: str) -> types.Tool | None

return tool

def call_tool(self, *, validate_input: bool = True):
def call_tool(self, *, validate_input: bool = True, validate_output: bool = True):
"""Register a tool call handler.

Args:
validate_input: If True, validates input against inputSchema. Default is True.
validate_output: If True, validates output against outputSchema. Default is True.

The handler validates input against inputSchema (if validate_input=True), calls the tool function,
and builds a CallToolResult with the results:
Expand Down Expand Up @@ -485,10 +486,11 @@ async def handler(req: types.CallToolRequest):
"Output validation error: outputSchema defined but no structured output returned"
)
else:
try:
jsonschema.validate(instance=maybe_structured_content, schema=tool.outputSchema)
except jsonschema.ValidationError as e:
return self._make_error_result(f"Output validation error: {e.message}")
if validate_output:
try:
jsonschema.validate(instance=maybe_structured_content, schema=tool.outputSchema)
except jsonschema.ValidationError as e:
return self._make_error_result(f"Output validation error: {e.message}")

# result
return types.ServerResult(
Expand Down
Loading