Skip to main content

micromegas/servers/mcp/
provider.rs

1use async_trait::async_trait;
2use micromegas_auth::types::AuthContext;
3use rmcp::model::{CallToolResult, JsonObject, Tool};
4
5/// What a tool provider knows about the caller of one MCP request.
6#[derive(Debug, Clone)]
7pub struct CallContext {
8    /// Identity validated by the host before the request reached the MCP service.
9    pub auth: AuthContext,
10    /// The full `Authorization` header value, forwarded to FlightSQL which validates it again.
11    pub authorization: String,
12    /// `User-Agent` of the MCP client, `unknown` when absent.
13    pub client_agent: String,
14    pub client_ip: String,
15}
16
17/// A group of tools served by the MCP route. The host mounts one or more providers, so a host
18/// that cannot offer a capability (e.g. screen management without the app database) leaves
19/// that provider out.
20#[async_trait]
21pub trait ToolProvider: Send + Sync {
22    /// Tools visible to this caller.
23    fn tools(&self, ctx: &CallContext) -> Vec<Tool>;
24
25    /// Runs `name`; `None` when this provider does not own the tool.
26    async fn call_tool(
27        &self,
28        ctx: &CallContext,
29        name: &str,
30        arguments: JsonObject,
31    ) -> Option<CallToolResult>;
32}
33
34/// Builds the JSON schema object of a tool's input.
35pub fn input_schema(
36    properties: serde_json::Value,
37    required: &[&str],
38) -> std::sync::Arc<JsonObject> {
39    let mut schema = JsonObject::new();
40    schema.insert("type".to_string(), "object".into());
41    schema.insert("properties".to_string(), properties);
42    schema.insert("required".to_string(), serde_json::json!(required));
43    std::sync::Arc::new(schema)
44}
45
46pub fn text_result(text: impl Into<String>) -> CallToolResult {
47    CallToolResult::success(vec![rmcp::model::ContentBlock::text(text)])
48}
49
50pub fn error_result(text: impl Into<String>) -> CallToolResult {
51    CallToolResult::error(vec![rmcp::model::ContentBlock::text(text)])
52}