Skip to content

feat: introduce CustomMcpServer class that handles JSONSchema #47

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

Merged
merged 1 commit into from
May 21, 2025

Conversation

kombucha
Copy link
Collaborator

@kombucha kombucha commented May 21, 2025

🤔 What

  • feat introduce CustomMcpServer class that handles JSONSchema directly

🤷 Why

The official SDK requires us to define tools' input with Zod, which causes a few problems:

  • We have to generate these Zod schemas dynamically from json schema, which is not always easy to do accurately especially when you have recursive schemas. You can checkout the readme of this lib for example which highlights that recursivity is only partially supported, and generating schemas at runtime only works by using eval 😬 .
  • Related to the above, enabling some tools would crash the server because of a bad recursion when generating the zod schema 🙈
  • Moreover, this Zod schema is then transformed back to a json schema anyway before being sent to the mcp client 🙃

I opened a PR on the official SDK repository, but I'm not sure if or when it will be merged (it's already full of conflicts because of refactorings happening on the main branch).
So I decided to do it in user land instead!

🔍 How

I'm leveraging the lower level Server class to implement my own McpServer. I mirrored the base implementation except I modified the inputSchema to accept a json schema instead. Tool arguments are now validated against this schema by using ajv.

I also had to modify the base openapi specs because the way it combined allOf and additionalProperties: false resulted in the validation rejecting actually valid inputs. I had to leverage unevaluatedProperties instead. I'll work on backporting these changes to the specs source of truth in https://github.com/algolia/api-clients-automation/tree/main/specs

🧪 Testing

I added a ton of integration tests around tool arguments validation
I have a test that makes sure that non of the tools can crash the server
I also tested manually with Claude desktop that calls are still working

@kombucha kombucha self-assigned this May 21, 2025
@kombucha kombucha force-pushed the feat-json-schema-driven-tools branch 6 times, most recently from e31aab6 to ccbc6d3 Compare May 21, 2025 16:41
@kombucha kombucha marked this pull request as ready for review May 21, 2025 17:39
@kombucha kombucha force-pushed the feat-json-schema-driven-tools branch from ccbc6d3 to 25755c0 Compare May 21, 2025 18:28
@kombucha kombucha merged commit 21b9159 into main May 21, 2025
3 checks passed
@kombucha kombucha deleted the feat-json-schema-driven-tools branch May 21, 2025 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant