feat(server): support json schema to define tool parameters #423
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds the ability to define tool parameters in json schema directly
Motivation and Context
Fixes #283
The
typescript-sdk
uses Zod to validate the parameters passed to tools and generates a json schema representation from it for the MCP client. There are cases where you might want to define tools directly with a JSON Schema: for example, when generating mcp tools dynamically from an OpenAPI Specification (example here). In these cases, you have to go from json schema, to Zod and back to json schema which is wasteful and hard to do without losing information along the way (for example, when you have recursive types).Example usage
How Has This Been Tested?
Breaking Changes
None, the changes are intended to only be additive.
Types of changes
Checklist
Additional context
args
(which is just marked asobject
).MCPServer#tool()
which accepts a configuration object instead of positional arguments. I only did that because I struggled to make the types work with positional arguments. Notably, I was losing type inference with theToolCallback
conditional type. Typescript does seem to infer types correctly when using the object version though. And while I understand it's completely subjective, I think having an object version makes for a nicer API (without removing previous options) and it would address Allow passing a Tool object to the server.tool() method #369mcp.ts
to keep the methods from growing too much from the additional complexity