自旋MCP触发器插件
一个Spin触发器插件,使WebAssembly组件能够充当模型上下文协议(MCP)服务器。
特性
- 完全支持MCP协议(工具、资源、提示)
- 基于HTTP传输的JSON-RPC 2.0
- 将MCP服务器轻松部署为Spin应用程序
- 与Claude Desktop和其他MCP客户端兼容
快速开始
1.安装插件和模板
来源:
git clone https://github.com/fastertools/spin-trigger-mcp.git
cd spin-trigger-mcp
make这将:
- 构建并安装
trigger-mcp插件 - 安装
mcp-rust创建新MCP组件的模板
验证安装:
spin plugins list
# Should show: trigger-mcp
spin templates list
# Should show: mcp-rust (MCP server component)2.创建新的MCP工具
使用模板创建新的MCP服务器:
spin new -t mcp-rust my-mcp-server
cd my-mcp-server这将创建一个项目结构,其中包含:
spin.toml-带MCP触发器配置的Spin应用程序清单src/lib.rs-带有MCP组件实现的Rust代码Cargo.toml-Rust依赖项,包括spin-mcp-sdk
生成的 spin.toml 看起来像:
spin_manifest_version = 2
[application]
name = "my-mcp-server"
version = "0.1.0"
[[trigger.mcp]]
component = "my-mcp-server"
route = "/mcp"
[component.my-mcp-server]
source = "target/wasm32-wasip1/release/my_mcp_server.wasm"
[component.my-mcp-server.build]
command = "cargo build --target wasm32-wasip1 --release"3.实施您的MCP工具
您的组件使用 mcp_component 宏来实现MCP接口:
use spin_mcp_sdk::{mcp_component, Request, Response, Tool, ToolResult, Error};
use serde_json::json;
#[mcp_component]
fn handle_request(request: Request) -> Response {
match request {
Request::ToolsList => {
Response::ToolsList(vec![
Tool {
name: "example_tool".to_string(),
description: "An example tool that echoes input".to_string(),
input_schema: json!({
"type": "object",
"properties": {
"message": {
"type": "string",
"description": "Message to echo"
}
},
"required": ["message"]
}).to_string(),
}
])
}
Request::ToolsCall(params) => {
match params.name.as_str() {
"example_tool" => {
// Parse arguments
let args: serde_json::Value = serde_json::from_str(¶ms.arguments)
.unwrap_or(json!({}));
let message = args.get("message")
.and_then(|v| v.as_str())
.unwrap_or("No message provided");
Response::ToolsCall(ToolResult::Text(
format!("Echo: {}", message)
))
}
_ => Response::ToolsCall(ToolResult::Error(Error {
code: -32602,
message: format!("Unknown tool: {}", params.name),
data: None,
}))
}
}
Request::ResourcesList => {
Response::ResourcesList(vec![])
}
Request::PromptsList => {
Response::PromptsList(vec![])
}
Request::Ping => Response::Pong,
_ => Response::Error(Error {
code: -32601,
message: "Method not found".to_string(),
data: None,
})
}
}4.构建并运行MCP服务器
spin build
spin up您的MCP服务器现在正在运行 http://localhost:3000/mcp 并且可以由MCP客户端访问。
MCP客户端配置
克劳德桌面
添加到您的Claude Desktop配置中:
{
"mcpServers": {
"demo": {
"url": "http://127.0.0.1:3000/mcp",
"transport": "http"
}
}
}API直接使用
# List available tools
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method": "tools/list", "id": 1}'
# Call a tool
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "example_tool",
"arguments": {"message": "Hello, MCP!"}
},
"id": 2
}'支持的MCP功能
- 工具:公开AI模型可以调用的函数
- 资源:共享文件或数据库模式等数据
- 鼓励:为AI交互提供提示模板
- 完整JSON-RPC 2.0:完成协议实施
例子
请参阅 examples/ 完整MCP服务器实现目录:
demo-mcp/-演示MCP基本功能的简单回声工具
要运行演示示例:
cd examples/demo-mcp
spin build
spin up然后进行测试:
# List tools
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method": "tools/list", "id": 1}'
# Call the echo tool
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "example_tool",
"arguments": {
"message": "Hello, MCP!"
}
},
"id": 2
}'发展
从源头构建
# Clone the repository
git clone https://github.com/fastertools/spin-trigger-mcp
cd spin-trigger-mcp
# Build the plugin
cargo build --release
# Package for distribution
spin pluginify运行测试
cargo test许可证
阿帕奇-2.0
