CI / UI Checks (pull_request) Successful in 12s
CI / Rust Checks (pull_request) Successful in 49m59s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 2s
CI / Deploy (pull_request) Skipped
177 lines
6.6 KiB
Rust
177 lines
6.6 KiB
Rust
//! Request-owned SDK handlers for the existing HTTP negotiation and ping routes.
|
|
use std::borrow::Cow;
|
|
|
|
use rmcp::{ErrorData, RoleServer, ServerHandler, model::*, service::RequestContext};
|
|
use serde_json::{Value, json};
|
|
|
|
use crate::{jsonrpc::SUPPORTED_PROTOCOL_VERSIONS, protocol::advertised_server_capabilities};
|
|
|
|
const SERVER_NAME: &str = "crank-mcp-server";
|
|
const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION");
|
|
|
|
#[derive(Clone)]
|
|
struct NegotiationHandler;
|
|
|
|
impl ServerHandler for NegotiationHandler {
|
|
fn supported_protocol_versions(&self) -> Cow<'static, [ProtocolVersion]> {
|
|
Cow::Owned(
|
|
SUPPORTED_PROTOCOL_VERSIONS
|
|
.iter()
|
|
.map(|v| version(v))
|
|
.collect(),
|
|
)
|
|
}
|
|
|
|
async fn initialize(
|
|
&self,
|
|
request: InitializeRequestParams,
|
|
_context: RequestContext<RoleServer>,
|
|
) -> Result<InitializeResult, ErrorData> {
|
|
let revision = request.protocol_version.as_str();
|
|
if !SUPPORTED_PROTOCOL_VERSIONS.contains(&revision) {
|
|
return Err(ErrorData::new(
|
|
ErrorCode(-32602),
|
|
format!("unsupported protocol version {revision}"),
|
|
None,
|
|
));
|
|
}
|
|
let caps = capabilities(&advertised_server_capabilities(revision));
|
|
#[cfg(test)]
|
|
super::dispatch_observer::sdk_initialize_handler_completed();
|
|
Ok(InitializeResult::new(caps)
|
|
.with_protocol_version(version(revision))
|
|
.with_server_info(Implementation::new(SERVER_NAME, SERVER_VERSION)))
|
|
}
|
|
|
|
async fn discover(
|
|
&self,
|
|
context: RequestContext<RoleServer>,
|
|
) -> Result<DiscoverResult, ErrorData> {
|
|
let revision = context
|
|
.extensions
|
|
.get::<ProtocolVersion>()
|
|
.ok_or_else(|| ErrorData::internal_error("discovery revision unavailable", None))?;
|
|
let revision = revision.as_str();
|
|
let result = DiscoverResult::new(
|
|
SUPPORTED_PROTOCOL_VERSIONS
|
|
.iter()
|
|
.map(|v| version(v))
|
|
.collect(),
|
|
capabilities(&advertised_server_capabilities(revision)),
|
|
)
|
|
.with_server_info(Implementation::new(SERVER_NAME, SERVER_VERSION))
|
|
.with_ttl_ms(0)
|
|
.with_cache_scope(CacheScope::Private);
|
|
#[cfg(test)]
|
|
super::dispatch_observer::sdk_discover_handler_completed();
|
|
Ok(result)
|
|
}
|
|
|
|
async fn ping(&self, _context: RequestContext<RoleServer>) -> Result<(), ErrorData> {
|
|
#[cfg(test)]
|
|
super::dispatch_observer::sdk_ping_handler_completed();
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
fn version(revision: &str) -> ProtocolVersion {
|
|
serde_json::from_value(Value::String(revision.to_owned()))
|
|
.expect("registered revisions are valid SDK versions")
|
|
}
|
|
|
|
fn capabilities(value: &Value) -> ServerCapabilities {
|
|
// Registry capabilities are the allowlist; the SDK must not supply defaults.
|
|
serde_json::from_value(value.clone()).expect("registered capabilities match SDK shapes")
|
|
}
|
|
|
|
async fn request_context() -> Result<RequestContext<RoleServer>, ErrorData> {
|
|
let ping: rmcp::model::ClientJsonRpcMessage =
|
|
serde_json::from_value(json!({"jsonrpc":"2.0", "id":1, "method":"ping"}))
|
|
.map_err(|_| ErrorData::internal_error("negotiation context unavailable", None))?;
|
|
let (transport, receiver) = rmcp::transport::OneshotTransport::<RoleServer>::new(ping);
|
|
let info = InitializeRequestParams::new(
|
|
ClientCapabilities::default(),
|
|
Implementation::new("crank-negotiation-context", "1"),
|
|
);
|
|
// Bootstrap only obtains a closed Peer; it must never run the request handler.
|
|
#[derive(Clone)]
|
|
struct Bootstrap;
|
|
impl ServerHandler for Bootstrap {}
|
|
let mut running = rmcp::service::serve_directly(Bootstrap, transport, Some(info));
|
|
let peer = running.peer().clone();
|
|
running
|
|
.close()
|
|
.await
|
|
.map_err(|_| ErrorData::internal_error("negotiation handler unavailable", None))?;
|
|
drop(receiver);
|
|
Ok(RequestContext::new(RequestId::Number(1), peer))
|
|
}
|
|
|
|
pub(super) async fn initialize(revision: &str) -> Result<Value, ErrorData> {
|
|
initialize_with(&NegotiationHandler, revision).await
|
|
}
|
|
|
|
async fn initialize_with(handler: &impl ServerHandler, revision: &str) -> Result<Value, ErrorData> {
|
|
let context = request_context().await?;
|
|
let params =
|
|
InitializeRequestParams::new(ClientCapabilities::default(), Implementation::new("", ""))
|
|
.with_protocol_version(version(revision));
|
|
let result = ServerHandler::initialize(handler, params, context).await?;
|
|
let mut value = serde_json::to_value(result)
|
|
.map_err(|_| ErrorData::internal_error("initialize result unavailable", None))?;
|
|
restore_capabilities(&mut value, revision);
|
|
#[cfg(test)]
|
|
super::dispatch_observer::sdk_initialize_handler_consumed();
|
|
Ok(value)
|
|
}
|
|
|
|
pub(super) async fn discover(revision: &str) -> Result<Value, ErrorData> {
|
|
discover_with(&NegotiationHandler, revision).await
|
|
}
|
|
|
|
async fn discover_with(handler: &impl ServerHandler, revision: &str) -> Result<Value, ErrorData> {
|
|
let mut context = request_context().await?;
|
|
context.extensions.insert(version(revision));
|
|
let result = ServerHandler::discover(handler, context).await?;
|
|
let mut value = serde_json::to_value(result)
|
|
.map_err(|_| ErrorData::internal_error("discover result unavailable", None))?;
|
|
restore_capabilities(&mut value, revision);
|
|
#[cfg(test)]
|
|
super::dispatch_observer::sdk_discover_handler_consumed();
|
|
Ok(value)
|
|
}
|
|
|
|
pub(super) async fn ping() -> Result<(), ErrorData> {
|
|
ping_with(&NegotiationHandler).await
|
|
}
|
|
|
|
async fn ping_with(handler: &impl ServerHandler) -> Result<(), ErrorData> {
|
|
let context = request_context().await?;
|
|
let result = ServerHandler::ping(handler, context).await;
|
|
#[cfg(test)]
|
|
super::dispatch_observer::sdk_ping_handler_consumed();
|
|
result
|
|
}
|
|
|
|
fn restore_capabilities(result: &mut Value, revision: &str) {
|
|
let advertised = advertised_server_capabilities(revision);
|
|
let represented =
|
|
serde_json::to_value(capabilities(&advertised)).expect("SDK capabilities serialize");
|
|
let caps = result["capabilities"]
|
|
.as_object_mut()
|
|
.expect("SDK capabilities serialize as object");
|
|
for (key, value) in advertised
|
|
.as_object()
|
|
.expect("registry capabilities are object")
|
|
{
|
|
// Restore only fields the SDK cannot represent, not removed typed capabilities.
|
|
if represented.get(key).is_none() {
|
|
caps.entry(key.clone()).or_insert_with(|| value.clone());
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "../rmcp_negotiation_handler_tests.rs"]
|
|
mod tests;
|