-
Notifications
You must be signed in to change notification settings - Fork 1.8k
add Tool.outputSchema and CallToolResult.structuredContent #685
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
base: main
Are you sure you want to change the base?
add Tool.outputSchema and CallToolResult.structuredContent #685
Conversation
…ly need an opt out for conversion to automatic conversion to DataContent
Tagging @bhosmer-ant I took a pass refactoring my previous merge request to this repo to support modelcontextprotocol/modelcontextprotocol#371 there's definitely some rough edges happy to discuss potential improvments. |
], | ||
], | ||
schema_func: Callable[..., dict[str, Any] | None] | None = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is rather clunky, however I could think of no other way to maintain backwards compa 8000 tibility with current python sdk that just assumes the function returns the result and not a protocol result as per the typescript version of this sdk which allows for a slightly cleaner implementation
Note failed test appears to be due to #744 |
Hi @davemssavage - really appreciate your work on this! Will be checking it out shortly, but meanwhile wanted to let you know it hasn't fallen off the map 😬 |
@bhosmer-ant no problem, just experimenting at the moment, but this feature is really useful in my experiments! Whilst I have your attention could I also highlight #628 for your consideration and #744 which is a bug that seems to be on main branch, the problem there is quite subtle, not least because it is intermittent so frustrating to test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @davemssavage - thank you for doing this! I've got to pause mid-review, will be back to it tomorrow but wanted to put up some comments in the meantime.
@@ -31,6 +31,7 @@ dependencies = [ | |||
"sse-starlette>=1.6.1", | |||
"pydantic-settings>=2.5.2", | |||
"uvicorn>=0.23.1; sys_platform != 'emscripten'", | |||
"jsonschema==4.23.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI internal-package-compatibility reasons we'll need to pin the jsonschema
version to 4.20.0
) | ||
|
||
|
||
_default_tool_output_validator: ToolOutputValidatorProvider = _python_circularity_hell |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This little dance (while hilariously rendered, kudos) should be avoidable using TYPE_CHECKING
- doing this up at the imports should let you use ClientSession here:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mcp.client.session import ClientSession
@@ -281,24 +317,33 @@ async def call_tool( | |||
arguments: dict[str, Any] | None = None, | |||
read_timeout_seconds: timedelta | None = None, | |||
progress_callback: ProgressFnT | None = None, | |||
validate_result: bool = True, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validation should be done or not based on whether the tool has an outputSchema
- shouldn't need an argument here I wouldn't think
@@ -418,3 +463,75 @@ async def _received_notification( | |||
await self._logging_callback(params) | |||
case _: | |||
pass | |||
|
|||
|
|||
class NoOpToolOutputValidator(ToolOutputValidationFnT): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure when we'd need this, if we're not running validation in the absence of an output schema
@davemssavage fyi I'm going to go ahead and bring this over the finish line - thanks again for all your work! |
Enable tool to return structured content and output schema.
Motivation and Context
As per discussion in modelcontextprotocol/modelcontextprotocol#371 there are use cases where being able to know the schema of a tool is useful prior to calling it. This patch implements the behaviour specified.
In addition it allows for an extensible validation mechanism to be introduced on the client side. The default behaviour is to check the response conforms to the schema that is cached by the client but leaves the door open to other validation options and different caching techniques.
How Has This Been Tested?
Local unit testing only so far, some testing with deployed server locally
Breaking Changes
None
Types of changes
Checklist
Additional context
Updates to documentation will be required if this patch is approved