|
| 1 | +//! SEP-2164: the resource-not-found error code follows the negotiated protocol version. |
| 2 | +//! |
| 3 | +//! `2026-07-28` and newer get the standard `INVALID_PARAMS` (-32602); older versions |
| 4 | +//! keep the legacy `RESOURCE_NOT_FOUND` (-32002). |
| 5 | +#![cfg(not(feature = "local"))] |
| 6 | +#![cfg(feature = "client")] |
| 7 | + |
| 8 | +use rmcp::{ |
| 9 | + ClientHandler, RoleServer, ServerHandler, ServiceError, ServiceExt, |
| 10 | + model::{ |
| 11 | + ClientInfo, ErrorCode, ErrorData, ProtocolVersion, ReadResourceRequestParams, |
| 12 | + ReadResourceResult, |
| 13 | + }, |
| 14 | + service::RequestContext, |
| 15 | +}; |
| 16 | + |
| 17 | +#[derive(Debug, Clone, Default)] |
| 18 | +struct ResourceServer; |
| 19 | + |
| 20 | +impl ServerHandler for ResourceServer { |
| 21 | + async fn read_resource( |
| 22 | + &self, |
| 23 | + _request: ReadResourceRequestParams, |
| 24 | + _context: RequestContext<RoleServer>, |
| 25 | + ) -> Result<ReadResourceResult, ErrorData> { |
| 26 | + Err(ErrorData::resource_not_found("resource not found", None)) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Debug, Clone)] |
| 31 | +struct VersionedClient { |
| 32 | + protocol_version: ProtocolVersion, |
| 33 | +} |
| 34 | + |
| 35 | +impl ClientHandler for VersionedClient { |
| 36 | + fn get_info(&self) -> ClientInfo { |
| 37 | + let mut info = ClientInfo::default(); |
| 38 | + info.protocol_version = self.protocol_version.clone(); |
| 39 | + info |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +async fn not_found_code(client_version: ProtocolVersion) -> ErrorCode { |
| 44 | + let (server_transport, client_transport) = tokio::io::duplex(4096); |
| 45 | + |
| 46 | + let server_handle = tokio::spawn(async move { |
| 47 | + ResourceServer |
| 48 | + .serve(server_transport) |
| 49 | + .await? |
| 50 | + .waiting() |
| 51 | + .await?; |
| 52 | + anyhow::Ok(()) |
| 53 | + }); |
| 54 | + |
| 55 | + let client = VersionedClient { |
| 56 | + protocol_version: client_version, |
| 57 | + } |
| 58 | + .serve(client_transport) |
| 59 | + .await |
| 60 | + .expect("client should connect"); |
| 61 | + |
| 62 | + let error = client |
| 63 | + .read_resource(ReadResourceRequestParams::new("missing://resource")) |
| 64 | + .await |
| 65 | + .expect_err("missing resource should error"); |
| 66 | + |
| 67 | + let code = match error { |
| 68 | + ServiceError::McpError(data) => data.code, |
| 69 | + other => panic!("expected McpError, got: {other:?}"), |
| 70 | + }; |
| 71 | + |
| 72 | + client.cancel().await.expect("client should cancel"); |
| 73 | + server_handle.await.expect("server task").expect("server"); |
| 74 | + code |
| 75 | +} |
| 76 | + |
| 77 | +#[tokio::test] |
| 78 | +async fn legacy_version_gets_resource_not_found_code() { |
| 79 | + assert_eq!( |
| 80 | + not_found_code(ProtocolVersion::V_2025_11_25).await, |
| 81 | + ErrorCode::RESOURCE_NOT_FOUND, |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +#[tokio::test] |
| 86 | +async fn sep_2164_version_gets_invalid_params_code() { |
| 87 | + assert_eq!( |
| 88 | + not_found_code(ProtocolVersion::V_2026_07_28).await, |
| 89 | + ErrorCode::INVALID_PARAMS, |
| 90 | + ); |
| 91 | +} |
0 commit comments