编码器_mcp_tool
用于代码生成代理的内存高效、超快的MCP工具。
特性
- 简单API -实现一个特性,自动获得RMCP集成、模式生成和历史跟踪
- 自动JSON模式 -衍生自
JsonSchema全局缓存实现零开销 - 内置工具历史记录 -即发即弃录制,具有持久的JSONL存储,从不阻止执行
- 行为注释 -声明工具语义(
read_only,destructive,idempotent,open_world) - 提示系统 -教代理如何使用带有对话风格提示的工具
- 性能优化 -模式缓存、Arc优化、后台I/O以实现最大吞吐量
- 全面错误 -
McpError枚举,可自动转换为RMCP错误类型 - 生产就绪 -线程安全,异步优先,具有原子文件操作和旋转
需求
- 每晚生锈 -此库使用Rust 2024版
- 东京 运行 时间 -异步执行由Tokio提供支持
安装
添加到您的 Cargo.toml:
[dependencies]
kodegen_mcp_tool = "0.1.0"或者从git安装:
[dependencies]
kodegen_mcp_tool = { git = "https://github.com/cyrup-ai/kodegen-mcp-tool" }快速开始
下面是一个实现工具的最小示例:
use kodegen_mcp_tool::{Tool, error::McpError};
use serde::{Deserialize, Serialize};
use schemars::JsonSchema;
use serde_json::Value;
use rmcp::model::{PromptArgument, PromptMessage};
// 1. Define your tool struct (holds dependencies)
pub struct EchoTool;
// 2. Define input arguments
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct EchoArgs {
message: String,
}
// 3. Define prompt arguments
#[derive(Deserialize, Serialize, JsonSchema)]
pub struct EchoPromptArgs {}
// 4. Implement the Tool trait
impl Tool for EchoTool {
type Args = EchoArgs;
type PromptArgs = EchoPromptArgs;
fn name() -> &'static str { "echo" }
fn description() -> &'static str { "Echoes back your message" }
async fn execute(&self, args: Self::Args) -> Result {
Ok(serde_json::json!({
"echo": args.message
}))
}
fn prompt_arguments() -> Vec
{
vec![]
}
async fn prompt(&self, _args: Self::PromptArgs) -> Result {
Ok(vec![])
}
}
// 5. Register with RMCP router
use rmcp::handler::server::router::RouterService;
#[tokio::main]
async fn main() {
// Initialize tool history
kodegen_mcp_tool::tool_history::init_global_history("my-server".to_string()).await;
// Create router and register tool
let router = RouterService::new("echo-server")
.tool(EchoTool.into_tool_route())
.prompt(EchoTool.into_prompt_route());
// Start server (example with stdio transport)
// ... your transport setup here ...
}行为注释
工具可以通过重写trait方法来声明它们的行为:
impl Tool for MyTool {
// ... other trait methods ...
fn read_only() -> bool { false } // Tool modifies state
fn destructive() -> bool { true } // Can delete/overwrite data
fn idempotent() -> bool { false } // Each call has different effect
fn open_world() -> bool { true } // Interacts with external systems
}这些注释会自动转换为RMCP ToolAnnotations 并帮助代理了解工具安全特性。
工具历史记录
自动跟踪所有工具的工具调用历史:
// Initialize once at startup
kodegen_mcp_tool::tool_history::init_global_history("server-id".to_string()).await;
// Access anywhere in your code
if let Some(history) = kodegen_mcp_tool::tool_history::get_global_history() {
let recent_calls = history.get_recent_calls(
100, // max results
0, // offset (negative = tail)
Some("my_tool"), // filter by tool name
None, // filter by timestamp
).await;
}特征:
- 即发即弃记录(从不阻止工具执行)
- 内存缓存(最后1000个条目)
- 持久JSONL存储
- 自动文件轮换5000个条目
- 后台磁盘I/O(1秒刷新间隔)
文档
生成并查看API文档:
cargo doc --open有关使用此代码库的更多详细指导,请参阅 CLAUDE.md.
建筑亮点
- 基于结构的工具 -工具是保持其依赖关系的有状态结构,而不是单例或静态函数
- 架构缓存 -JSON模式每种工具类型计算一次,并使用全局缓存
LazyLock - 电弧优化 -
arc_into_tool_route()变体避免了预包装工具的双圆弧分配 - 后台处理 -工具历史记录对所有磁盘I/O使用专用后台任务
许可证
双重许可:
- Apache许可证,版本2.0(特许通行证 或http://www.apache.org/licenses/LICENSE-2.0)
- MIT许可证(许可证-麻省理工学院 或http://opensource.org/licenses/MIT)
由您选择。
链接
- 首页: https://kodegen.ai
- 仓库:
- 模型上下文协议: https://modelcontextprotocol.io
- RMCP-SDK:
______________________________________________________________________
KODEGEN -建造于 青榨槭
