This commit is contained in:
2026-05-22 08:40:04 +02:00
parent 749638b75c
commit 777fcf168e
13 changed files with 74 additions and 56 deletions
+9 -8
View File
@@ -3,15 +3,13 @@ mod tools;
use crate::{api::auth::MaybeBearerToken, app::state::AppState};
use protocol::{
ApiResponse, InitializeResult, JSONRPC_VERSION, jsonrpc_error, jsonrpc_notification_ok,
jsonrpc_ok,
ApiResponse, InitializeResult, JSONRPC_VERSION, JsonRpcRequest, jsonrpc_error,
jsonrpc_notification_ok, jsonrpc_ok,
};
use rocket::{State, http::Status, post, serde::json::Json};
use serde_json::Value;
use tools::{handle_tool_call, tool_definitions};
pub use protocol::JsonRpcRequest;
#[post("/mcp", data = "<body>")]
pub async fn route(
body: Json<JsonRpcRequest>,
@@ -32,10 +30,13 @@ pub async fn route(
match request.method.as_str() {
"initialize" => jsonrpc_ok(request.id, InitializeResult::new()),
"notifications/initialized" => jsonrpc_notification_ok(),
"tools/list" => match token.0 {
Some(_) => jsonrpc_ok(request.id, tool_definitions()),
None => missing_authorization(request.id),
},
"tools/list" => {
if token.0.is_some() {
jsonrpc_ok(request.id, tool_definitions())
} else {
missing_authorization(request.id)
}
}
"tools/call" => {
handle_tool_call(request.id, request.params, token.0.as_deref(), state).await
}
+11 -5
View File
@@ -65,11 +65,7 @@ impl InitializeResult {
pub fn new() -> Self {
Self {
protocol_version: MCP_PROTOCOL_VERSION,
capabilities: ServerCapabilities {
tools: ToolsCapability {
list_changed: false,
},
},
capabilities: ServerCapabilities::new(),
server_info: ServerInfo {
name: env!("CARGO_PKG_NAME"),
version: env!("CARGO_PKG_VERSION"),
@@ -78,6 +74,16 @@ impl InitializeResult {
}
}
impl ServerCapabilities {
fn new() -> Self {
Self {
tools: ToolsCapability {
list_changed: false,
},
}
}
}
pub fn jsonrpc_ok<T: Serialize>(id: Option<Value>, result: T) -> ApiResponse {
match serde_json::to_string(&JsonRpcResponse {
jsonrpc: JSONRPC_VERSION,
+6 -5
View File
@@ -178,16 +178,17 @@ fn json_tool_result<T: Serialize>(id: Option<Value>, payload: &T) -> ApiResponse
impl ToolCallResult {
fn success(text: String) -> Self {
Self {
content: vec![TextContent::text(text)],
is_error: false,
}
Self::new(text, false)
}
fn error(text: String) -> Self {
Self::new(text, true)
}
fn new(text: String, is_error: bool) -> Self {
Self {
content: vec![TextContent::text(text)],
is_error: true,
is_error,
}
}
}