This commit is contained in:
parent
554082f88c
commit
b4bbbd28ef
45
README.md
45
README.md
@ -1,43 +1,4 @@
|
|||||||
# Mcp
|
# Ruby MCP Client Implementation
|
||||||
|
|
||||||
TODO: Delete this and the text below, and describe your gem
|
This is a small MCP client implementation for ruby. It is probably not what you want to actually
|
||||||
|
use as it needlessly wraps a rust implementation of JSON-RPC as a learning exercise.
|
||||||
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mcp`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
||||||
|
|
||||||
Install the gem and add to the application's Gemfile by executing:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
||||||
```
|
|
||||||
|
|
||||||
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
||||||
```
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
TODO: Write usage instructions here
|
|
||||||
|
|
||||||
## Development
|
|
||||||
|
|
||||||
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
||||||
|
|
||||||
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
||||||
|
|
||||||
## Contributing
|
|
||||||
|
|
||||||
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mcp. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/mcp/blob/main/CODE_OF_CONDUCT.md).
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
||||||
|
|
||||||
## Code of Conduct
|
|
||||||
|
|
||||||
Everyone interacting in the Mcp project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/mcp/blob/main/CODE_OF_CONDUCT.md).
|
|
||||||
|
|||||||
@ -3,6 +3,7 @@ use tokio::process::Command;
|
|||||||
use crate::mcp_client::McpClient;
|
use crate::mcp_client::McpClient;
|
||||||
use jsonrpsee::core::client::ClientT;
|
use jsonrpsee::core::client::ClientT;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
use magnus::prelude::*;
|
||||||
|
|
||||||
mod mcp_client;
|
mod mcp_client;
|
||||||
mod types;
|
mod types;
|
||||||
@ -10,15 +11,12 @@ mod rpc_helpers;
|
|||||||
mod stdio_transport;
|
mod stdio_transport;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
cell::RefCell,
|
|
||||||
fmt,
|
|
||||||
hash::{Hash, Hasher},
|
hash::{Hash, Hasher},
|
||||||
};
|
};
|
||||||
|
|
||||||
use magnus::{function, method, prelude::*, scan_args::{get_kwargs, scan_args}, typed_data, Error, Ruby, Value};
|
use magnus::{function, method, prelude::*, scan_args::{get_kwargs, scan_args}, typed_data, Error, RHash, Ruby, Symbol, TryConvert, Value};
|
||||||
use serde_magnus::serialize;
|
use serde_magnus::serialize;
|
||||||
use crate::types::{Implementation, InitializeRequestParams, InitializeResult};
|
use crate::types::{CallToolRequestParams, Implementation, InitializeRequestParams, InitializeResult};
|
||||||
use crate::types::builder::Tool;
|
|
||||||
|
|
||||||
// Create global runtime
|
// Create global runtime
|
||||||
static RUNTIME: Lazy<tokio::runtime::Runtime> = Lazy::new(|| {
|
static RUNTIME: Lazy<tokio::runtime::Runtime> = Lazy::new(|| {
|
||||||
@ -83,12 +81,32 @@ impl McpClientRb {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn call_rpc(&self, method: &str, params: &[&str]) -> Result<Value, magnus::Error> {
|
fn call_tool(&self, values: &[Value]) -> Result<Value, magnus::Error> {
|
||||||
// RUNTIME.block_on(async {
|
let args = scan_args::<(Value,), (), (), (), RHash, ()>(values)?;
|
||||||
// self.client.client.request(method, params).await
|
let ((name,)) = args.required;
|
||||||
// .map_err(|e| magnus::Error::new(magnus::exception::runtime_error(), e.to_string()))
|
let kwargs: RHash = args.keywords;
|
||||||
// })
|
let kwargs: serde_json::Map<String, serde_json::Value> = serde_magnus::deserialize(kwargs)?;
|
||||||
// }
|
|
||||||
|
let name = match Symbol::from_value(name) {
|
||||||
|
Some(symbol) => symbol.name()?.to_string(),
|
||||||
|
None => String::try_convert(name)?,
|
||||||
|
};
|
||||||
|
|
||||||
|
RUNTIME.block_on(async {
|
||||||
|
let a = self.client.call_tool::<serde_json::Value>(CallToolRequestParams {
|
||||||
|
name,
|
||||||
|
arguments: kwargs,
|
||||||
|
}).await;
|
||||||
|
|
||||||
|
match a {
|
||||||
|
Ok(a) => Ok(serde_magnus::serialize(&a)?),
|
||||||
|
Err(e) => Err(Error::new(
|
||||||
|
magnus::exception::runtime_error(),
|
||||||
|
e.to_string(),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[magnus::init]
|
#[magnus::init]
|
||||||
@ -99,6 +117,7 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|||||||
client_class.define_singleton_method("new", function!(McpClientRb::new, 2))?;
|
client_class.define_singleton_method("new", function!(McpClientRb::new, 2))?;
|
||||||
client_class.define_method("connect", method!(McpClientRb::connect, 0))?;
|
client_class.define_method("connect", method!(McpClientRb::connect, 0))?;
|
||||||
client_class.define_method("list_tools", method!(McpClientRb::list_tools, 0))?;
|
client_class.define_method("list_tools", method!(McpClientRb::list_tools, 0))?;
|
||||||
|
client_class.define_method("call_tool", method!(McpClientRb::call_tool, -1))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,7 +31,7 @@ impl McpClient {
|
|||||||
Ok(tools)
|
Ok(tools)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn call_tool<T: serde::de::DeserializeOwned>(&self, params: CallToolRequestParams) -> Result<T, anyhow::Error> {
|
pub async fn call_tool<T: serde::de::DeserializeOwned>(&self, params: CallToolRequestParams) -> Result<T, anyhow::Error> {
|
||||||
Ok(self.client.request("tools/call", params.to_rpc()).await?)
|
Ok(self.client.request("tools/call", params.to_rpc()).await?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
24
lib/mcp.rb
24
lib/mcp.rb
@ -4,7 +4,29 @@ require_relative "mcp/version"
|
|||||||
|
|
||||||
module Mcp
|
module Mcp
|
||||||
class Error < StandardError; end
|
class Error < StandardError; end
|
||||||
# Your code goes here...
|
|
||||||
|
class Client
|
||||||
|
def tools
|
||||||
|
ToolsProxy.new(self)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ToolsProxy
|
||||||
|
def initialize(client)
|
||||||
|
@client = client
|
||||||
|
@tools = client.list_tools
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def respond_to_missing?(name, include_private = false)
|
||||||
|
@tools.any? { |tool| tool["name"] == name.to_s } || super
|
||||||
|
end
|
||||||
|
|
||||||
|
def method_missing(name, **kwargs)
|
||||||
|
@client.call_tool(name, **kwargs)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
require_relative "mcp/mcp"
|
require_relative "mcp/mcp"
|
||||||
|
|||||||
@ -6,6 +6,9 @@ RSpec.describe Mcp do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "does something useful" do
|
it "does something useful" do
|
||||||
expect(false).to eq(true)
|
a = Mcp::Client.new("/Users/joshuacoles/.local/bin/mcp-server-fetch", [])
|
||||||
|
expect(a.connect).to eq(true)
|
||||||
|
puts a.list_tools
|
||||||
|
puts a.tools.fetch(url: 'http://example.com')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user