cleanup
This commit is contained in:
@@ -22,9 +22,11 @@ impl<'r> FromRequest<'r> for BearerToken {
|
||||
let Some(header) = request.headers().get_one(AUTHORIZATION_HEADER) else {
|
||||
return Outcome::Error((Status::Unauthorized, "Missing Authorization header"));
|
||||
};
|
||||
|
||||
let Some(token) = header.strip_prefix(BEARER_PREFIX) else {
|
||||
return Outcome::Error((Status::Unauthorized, "Invalid Authorization header format"));
|
||||
};
|
||||
|
||||
Outcome::Success(BearerToken(token.to_owned()))
|
||||
}
|
||||
}
|
||||
|
||||
+9
-8
@@ -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
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
pub mod auth;
|
||||
pub mod catchers;
|
||||
pub mod mcp;
|
||||
Reference in New Issue
Block a user