场景
MCP的出现大大丰富了LLM的功能,对于存量系统,我们希望能让模型调用已有的接口,以最小的成本让AI能够获取系统内部数据。因此我们开发了一个名为http-api-call
的MCP Server,来支持模型到内部API的调用
实现方案
使用用标准的MCP协议,重写call_tool
和list_tool
。
- call_tool
在这里实现调用工具的逻辑,将请求信息转发给已有系统的后端接口调用 - list_tool
在这里实现工具列表的查询,返回以及配置的API即可
这样我们将工具列表封装为tool_message,传递给模型,即可让模型自动选择合适的API调用。
#[derive(Debug, Clone)]
pub struct McpService;impl McpService {pub fn new() -> Self {Self {}}/// 添加或更新APIfn add_or_update_api(&self, req: Api) -> Res<()> {}/// 删除APIfn remove_api(&self, req: RemoveApiReq) -> Res<()> {}/// 查询API列表fn list_api(&self, req: Option<ListApiReq>) -> Res<PageRes<Api>> {}
}impl ServerHandler for McpService {async fn call_tool(&self,request: CallToolRequestParam,_context: RequestContext<RoleServer>,) -> Result<CallToolResult, Error> {// 请求转发}fn list_tools(&self,_request: PaginatedRequestParam,_context: RequestContext<RoleServer>,) -> impl Future<Output = Result<ListToolsResult, Error>> + Send + '_ {//返回API列表}
}
配置API:
在模型中使用API: