sse endpoint
This commit is contained in:
@@ -24,3 +24,5 @@ docker build -t server .
|
|||||||
- requires `X-Device-ID: <device-id>`
|
- requires `X-Device-ID: <device-id>`
|
||||||
- `POST /mcp` for MCP/JSON-RPC requests
|
- `POST /mcp` for MCP/JSON-RPC requests
|
||||||
- typically uses `Authorization: Bearer <token>`
|
- typically uses `Authorization: Bearer <token>`
|
||||||
|
- `GET /mcp` for MCP server-sent events
|
||||||
|
- provides a minimal SSE stream for MCP clients that probe or require it
|
||||||
|
|||||||
+25
-1
@@ -6,10 +6,34 @@ use protocol::{
|
|||||||
ApiResponse, InitializeResult, JSONRPC_VERSION, JsonRpcRequest, jsonrpc_error,
|
ApiResponse, InitializeResult, JSONRPC_VERSION, JsonRpcRequest, jsonrpc_error,
|
||||||
jsonrpc_notification_ok, jsonrpc_ok,
|
jsonrpc_notification_ok, jsonrpc_ok,
|
||||||
};
|
};
|
||||||
use rocket::{State, http::Status, post, serde::json::Json};
|
use rocket::{
|
||||||
|
Shutdown, State, get,
|
||||||
|
http::Status,
|
||||||
|
post,
|
||||||
|
response::stream::{Event, EventStream},
|
||||||
|
serde::json::Json,
|
||||||
|
};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use tools::{handle_tool_call, tool_definitions};
|
use tools::{handle_tool_call, tool_definitions};
|
||||||
|
|
||||||
|
const MCP_MESSAGE_ENDPOINT_EVENT: &str = "endpoint";
|
||||||
|
|
||||||
|
#[get("/mcp")]
|
||||||
|
pub fn sse(shutdown: Shutdown) -> EventStream![] {
|
||||||
|
EventStream! {
|
||||||
|
yield Event::json(&serde_json::json!({ "path": "/mcp" })).event(MCP_MESSAGE_ENDPOINT_EVENT);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
tokio::select! {
|
||||||
|
_ = shutdown.clone() => break,
|
||||||
|
_ = tokio::time::sleep(std::time::Duration::from_secs(15)) => {
|
||||||
|
yield Event::comment("keepalive");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[post("/mcp", data = "<body>")]
|
#[post("/mcp", data = "<body>")]
|
||||||
pub async fn route(
|
pub async fn route(
|
||||||
body: Json<JsonRpcRequest>,
|
body: Json<JsonRpcRequest>,
|
||||||
|
|||||||
+4
-1
@@ -33,7 +33,10 @@ pub async fn apply_schema(database: &SqlitePool) -> Result<(), sqlx::Error> {
|
|||||||
pub fn build_rocket(database: SqlitePool) -> rocket::Rocket<rocket::Build> {
|
pub fn build_rocket(database: SqlitePool) -> rocket::Rocket<rocket::Build> {
|
||||||
rocket::build()
|
rocket::build()
|
||||||
.manage(build_state(database))
|
.manage(build_state(database))
|
||||||
.mount("/", routes![transport::socket::connect, api::mcp::route])
|
.mount(
|
||||||
|
"/",
|
||||||
|
routes![transport::socket::connect, api::mcp::route, api::mcp::sse],
|
||||||
|
)
|
||||||
.register("/", rocket::catchers![api::catchers::default_catcher])
|
.register("/", rocket::catchers![api::catchers::default_catcher])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user