Skip to content

Adding MCP Annotation and MCP Generator #122

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 10 commits into
base: main
Choose a base branch
from

Conversation

nelsoncampos-cloudwalk
Copy link

Hi Dart team! This PR adds automatic code‑generation for MCP servers, making setup quicker and cleaner. Hope it helps—thanks for the awesome project!

  • Introduced mcp_annotations for declaring MCP tools and servers with annotations.
  • Added mcp_codegen for generating boilerplate code from annotations.
  • Updated README to include new packages and their descriptions.
  • Created a demo server example showcasing the use of MCP annotations and code generation.
  • Added build configuration for the code generation process.

Here it's a demo:

Screen.Recording.2025-05-10.at.14.16.19.1.mp4

  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.
Contribution guidelines:

Note that many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.

- Introduced `mcp_annotations` for declaring MCP tools and servers with annotations.
- Added `mcp_codegen` for generating boilerplate code from annotations.
- Updated README to include new packages and their descriptions.
- Created a demo server example showcasing the use of MCP annotations and code generation.
- Added build configuration for the code generation process.
- Simplified the constructor of MCPDemoServer to a constant constructor.
- Streamlined the add method to use a concise arrow function for better readability.
- Introduced a new code generation framework for MCP servers and tools.
- Added `HandlerBuilder`, `ServerBuilder`, `SchemaBuilder`, and `CodeEmitter` classes to facilitate the generation of server and tool code.
- Implemented `AnnotationScanner` and `ToolScanner` for scanning libraries for MCP annotations.
- Refactored `MCPGenerator` to utilize the new builders and scanners for cleaner code generation.
- Added utility methods for handling annotations and generating schemas for tool parameters.
- Updated `HandlerBuilder` and `ServerBuilder` to improve code generation logic, including changes to argument handling.
- Introduced new test files for `HandlerBuilder`, `SchemaBuilder`, `ServerBuilder`, and scanners to ensure proper functionality and coverage.
- Added utility tests for annotation detection and tool registration.
- Removed unused element warnings and simplified input schema property definitions for better clarity.
- Streamlined the data transformation logic in the server setup for improved readability.
- Cleaned up comments to enhance code maintainability.
- Added copyright headers to multiple test files in the mcp_codegen package to comply with licensing requirements.
- Changed the `myServer` function to a `MyServer` class with a constant constructor and an `add` method annotated with `@MCPTool()`.
- Updated the test expectation to reflect the new class name.
- Updated `pubspec.yaml` in the demo server to use versioned dependencies for `dart_mcp` and `mcp_annotations`.
- Added `mcp_codegen` as a dev dependency.
- Refactored `main.dart` to simplify the `add` method and removed the unused `strlen` method.
- Added changelogs and licenses for `mcp_annotations` and `mcp_codegen` packages, marking their initial release.
- Updated `pubspec.yaml` for both packages to reflect version changes.
@jakemac53
Copy link
Contributor

Hi! This looks cool and definitely reduces some of the boilerplate around setting up servers.

I think the main question is just whether it is something we want to own/support at this time - code generators do tend to be a fair bit of maintenance due to keeping up with the language and analyzer API evolution, as well as just being fairly complex and tending to get a lot of requests for new features so they tend to grow in complexity a lot.

With the rapidly evolving nature of dart_mcp I also worry we willl have lots of churn in the code generator.

@kenzieschmoll thoughts?

@nelsoncampos-cloudwalk
Copy link
Author

Thanks for the thoughtful feedback @jakemac53 — I totally understand your concerns about ownership and maintenance, especially given how quickly Dart and dart_mcp are evolving.

I'm happy to handle ongoing maintenance for the generator here, including keeping it updated with language changes and analyzer APIs. If I ever need to step away, I'll either find a new maintainer or help gracefully deprecate the package.

If integrating the generator directly into dart-lang/ai feels heavy, we could also consider:

  • Option A: Keeping mcp_codegen in a separate repo under my GitHub org and publishing it independently to pub.dev, with just a link from the README here.
  • Option B: Landing it behind an experimental flag initially, omitting it from the next stable release, giving us time to gauge adoption before fully committing.

I'm really engaged with the project, so I'm also happy to contribute to other issues or priorities you might have—just let me know where else I could help!

Looking forward to hearing your thoughts, especially from @kenzieschmoll!

@jakemac53
Copy link
Contributor

jakemac53 commented May 13, 2025

I think from my perspective, letting you run with the package in your own repo but mentioning it in the readme is a good option for us (and you actually, you won't have to wait on us for reviews which might be a bit slow due to this being lower priority for us than the core SDK bits).

But, lets see what Kenzie says as well 👍

@kenzieschmoll
Copy link
Contributor

I agree with @jakemac53. I think the best path forward is to leave this in a separate package in your own repo. We can discuss adding a reference in the dart_mcp README.

For this reference, I would just want to be clear about our promises WRT to production quality and ongoing support for any third party packages we link to in the README, basically stating that we, the Dart team, do not make any promises about quality, support, or maintenance, and that users should perform their own investigation before using any of the linked third party packages. This note should also clarify that we may link to other third party packages in this section on a case-by-case basis.

@jakemac53
Copy link
Contributor

@nelsoncampos-cloudwalk how does that sound?

@nelsoncampos-cloudwalk
Copy link
Author

This sounds good — thanks for the thoughtful discussion @jakemac53 and @kenzieschmoll!

I'll create a separate repo for the generator and share the link here soon. Once that's set, we can close this PR. Then, I can either open a new PR to add the link to the README or leave it to you — whatever's easier for you.

Thanks again for your time, support, and guidance!

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.

3 participants