-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add mcp tool provider #71
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| //! Adapt an in-process MCP server's tools as loopctl `Tool` impls. | ||
| //! | ||
| //! Builds a tiny rmcp server (with the `#[tool_router]` / `#[tool]` macros), | ||
| //! connects an [`McpToolProvider`] to it over a `tokio::io::duplex`, discovers | ||
| //! its tools, registers them into a [`ToolRegistry`], and calls one to prove | ||
| //! the round-trip works end-to-end. | ||
| //! | ||
| //! ```sh | ||
| //! cargo run --example mcp-adapter --features mcp | ||
| //! ``` | ||
|
|
||
| #![allow( | ||
| clippy::expect_used, | ||
| clippy::panic, | ||
| clippy::indexing_slicing, | ||
| clippy::missing_errors_doc, | ||
| clippy::missing_panics_doc, | ||
| dead_code | ||
| )] | ||
|
|
||
| use loopctl::mcp::{McpClient, McpToolProvider}; | ||
| use loopctl::tool::{ToolContext, ToolRegistry}; | ||
| use rmcp::handler::server::ServerHandler; | ||
| use rmcp::handler::server::router::tool::ToolRouter; | ||
| use rmcp::{tool, tool_handler, tool_router}; | ||
| use serde_json::json; | ||
|
|
||
| /// An rmcp server exposing one `greet` tool. | ||
| #[derive(Clone)] | ||
| struct GreetServer { | ||
| router: ToolRouter<Self>, | ||
| } | ||
|
|
||
| #[tool_router] | ||
| impl GreetServer { | ||
| fn new() -> Self { | ||
| Self { | ||
| router: Self::tool_router(), | ||
| } | ||
| } | ||
|
|
||
| #[tool(description = "Return a friendly greeting")] | ||
| async fn greet(&self) -> String { | ||
| "hello, world!".to_string() | ||
| } | ||
| } | ||
|
|
||
| #[tool_handler] | ||
| impl ServerHandler for GreetServer {} | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| // 1. Connect an rmcp client to the in-process server and run the MCP | ||
| // initialize handshake. This is the only constructor L-12 ships; real | ||
| // transports (stdio, HTTP/SSE) arrive in a later release. | ||
| let client = McpClient::in_process(GreetServer::new()) | ||
| .await | ||
| .expect("client initialize"); | ||
|
|
||
| // 2. Discover the server's tools and register them. | ||
| let provider = McpToolProvider::connect(client, None) | ||
| .await | ||
| .expect("connect + list_tools"); | ||
| let mut registry = ToolRegistry::new(); | ||
| provider.register_into(&mut registry); | ||
| println!("discovered {} tool(s):", registry.len()); | ||
| for schema in registry.all_schemas() { | ||
| println!(" - {} : {}", schema.tool, schema.description); | ||
| } | ||
|
|
||
| // 3. Call the adapted `greet` tool through the registry, exactly as the | ||
| // agent loop would call any native loopctl tool. | ||
| let greet = registry.get("greet").expect("greet registered"); | ||
| let ctx = ToolContext::default(); | ||
| let out = greet.call(json!({}), &ctx).await.expect("greet call"); | ||
| println!("greet -> {}", out.text_content()); | ||
| assert!(!out.is_error); | ||
| assert_eq!(out.text_content(), "hello, world!"); | ||
| println!("OK"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.