feat: add support for custom requests (#590)

#580 and #556 introduced support for custom notifications,
so this PR takes the next logical step and adds support for custom requests:

- Introduces `CustomRequest` and `CustomResult` model types, wires them into the client/server
  request and result unions, and allows `ClientRequest::method()` to return the dynamic method
  name.
- Implements serde and meta handling for `CustomRequest` so `_meta` is carried through
  extensions; adds default `on_custom_request` handlers that return `METHOD_NOT_FOUND` unless
  overridden.
- Updates JSON schema fixtures to include the new request/result shapes and `EmptyObject`
  strictness.
- Adds tests for custom request roundtrips and end-to-end client↔server handling.
- Focused integration test in `crates/rmcp/tests/test_custom_request.rs`.

For additional testing, I used this locally to update Codex to use a custom
request instead of a custom notification so that it gets an "ack" from the MCP
server to ensure it has processed the update before sending more messages:
https://github.com/openai/codex/pull/8142.
This commit is contained in:
Michael Bolin 2025-12-18 09:41:06 -08:00 • committed by GitHub
commit e0faf1ed51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -55,6 +55,7 @@ macro_rules! object {
///
/// without returning any specific data.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Copy, Eq)]
#[serde(deny_unknown_fields)]
#[cfg_attr(feature = "server", derive(schemars::JsonSchema))]
pub struct EmptyObject {}
@ -606,6 +607,23 @@ impl From<EmptyResult> for () {
fn from(_value: EmptyResult) {}
}
/// A catch-all response either side can use for custom requests.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(transparent)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct CustomResult(pub Value);
impl CustomResult {
pub fn new(result: Value) -> Self {
Self(result)
}
/// Deserialize the result into a strongly-typed structure.
pub fn result_as<T: DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
serde_json::from_value(self.0.clone())
}
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
@ -661,6 +679,40 @@ impl CustomNotification {
}
}
/// A catch-all request either side can use to send custom messages to its peer.
///
/// This preserves the raw `method` name and `params` payload so handlers can
/// deserialize them into domain-specific types.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct CustomRequest {
pub method: String,
pub params: Option<Value>,
/// extensions will carry anything possible in the context, including [`Meta`]
///
/// this is similar with the Extensions in `http` crate
#[cfg_attr(feature = "schemars", schemars(skip))]
pub extensions: Extensions,
}
impl CustomRequest {
pub fn new(method: impl Into<String>, params: Option<Value>) -> Self {
Self {
method: method.into(),
params,
extensions: Extensions::default(),
}
}
/// Deserialize `params` into a strongly-typed structure.
pub fn params_as<T: DeserializeOwned>(&self) -> Result<Option<T>, serde_json::Error> {
self.params
.as_ref()
.map(|params| serde_json::from_value(params.clone()))
.transpose()
}
}
const_string!(InitializeResultMethod = "initialize");
/// # Initialization
/// This request is sent from the client to the server when it first connects, asking it to begin initialization.
@ -1757,11 +1809,12 @@ ts_union!(
| SubscribeRequest
| UnsubscribeRequest
| CallToolRequest
| ListToolsRequest;
| ListToolsRequest
| CustomRequest;
);
impl ClientRequest {
pub fn method(&self) -> &'static str {
pub fn method(&self) -> &str {
match &self {
ClientRequest::PingRequest(r) => r.method.as_str(),
ClientRequest::InitializeRequest(r) => r.method.as_str(),
@ -1776,6 +1829,7 @@ impl ClientRequest {
ClientRequest::UnsubscribeRequest(r) => r.method.as_str(),
ClientRequest::CallToolRequest(r) => r.method.as_str(),
ClientRequest::ListToolsRequest(r) => r.method.as_str(),
ClientRequest::CustomRequest(r) => r.method.as_str(),
}
}
}
@ -1790,7 +1844,12 @@ ts_union!(
);
ts_union!(
export type ClientResult = box CreateMessageResult | ListRootsResult | CreateElicitationResult | EmptyResult;
export type ClientResult =
box CreateMessageResult
| ListRootsResult
| CreateElicitationResult
| EmptyResult
| CustomResult;
);
impl ClientResult {
@ -1806,7 +1865,8 @@ ts_union!(
| PingRequest
| CreateMessageRequest
| ListRootsRequest
| CreateElicitationRequest;
| CreateElicitationRequest
| CustomRequest;
);
ts_union!(
@ -1834,6 +1894,7 @@ ts_union!(
| ListToolsResult
| CreateElicitationResult
| EmptyResult
| CustomResult
;
);
@ -1960,6 +2021,40 @@ mod tests {
assert_eq!(json, raw);
}
#[test]
fn test_custom_request_roundtrip() {
let raw = json!( {
"jsonrpc": JsonRpcVersion2_0,
"id": 42,
"method": "requests/custom",
"params": {"foo": "bar"},
});
let message: ClientJsonRpcMessage =
serde_json::from_value(raw.clone()).expect("invalid request");
match &message {
ClientJsonRpcMessage::Request(JsonRpcRequest { id, request, .. }) => {
assert_eq!(id, &RequestId::Number(42));
match request {
ClientRequest::CustomRequest(custom) => {
let expected_request = json!({
"method": "requests/custom",
"params": {"foo": "bar"},
});
let actual_request =
serde_json::to_value(custom).expect("serialize custom request");
assert_eq!(actual_request, expected_request);
}
other => panic!("Expected custom request, got: {other:?}"),
}
}
other => panic!("Expected request, got: {other:?}"),
}
let json = serde_json::to_value(message).expect("valid json");
assert_eq!(json, raw);
}
#[test]
fn test_request_conversion() {
let raw = json!( {