pub struct JsonRpcService<S> { /* private fields */ }Expand description
Service that handles JSON-RPC framing.
Wraps an MCP service and handles JSON-RPC request/response conversion.
Supports single requests for every implemented revision. Request batches
are accepted only after the exact runtime revision is known to be
2025-03-26; later MCP revisions removed top-level JSON-RPC batching.
Can be created directly via JsonRpcService::new or through the
JsonRpcLayer for ServiceBuilder composition.
§Example
use tower_mcp::{McpRouter, JsonRpcService};
let router = McpRouter::new().server_info("my-server", "1.0.0");
let service = JsonRpcService::new(router);Implementations§
Source§impl<S> JsonRpcService<S>
impl<S> JsonRpcService<S>
Sourcepub fn with_extensions(self, ext: Extensions) -> Self
pub fn with_extensions(self, ext: Extensions) -> Self
Set extensions to inject into every RouterRequest created by this service.
This is used by transports to bridge data (e.g., TokenClaims) from the
HTTP/WebSocket layer into the MCP request pipeline.
Sourcepub fn protocol_support(self, support: ProtocolSupport) -> Self
pub fn protocol_support(self, support: ProtocolSupport) -> Self
Set the exact protocol versions this service accepts and advertises.
The default enables every protocol implementation compiled into
tower-mcp. Transports expose the same policy so applications can
narrow support per server instance.
Sourcepub fn protocol_versions<I, V>(
self,
versions: I,
) -> Result<Self, ProtocolSupportError>
pub fn protocol_versions<I, V>( self, versions: I, ) -> Result<Self, ProtocolSupportError>
Construct and set an exact runtime protocol-version allow-list.
Sourcepub async fn call_single(
&mut self,
req: JsonRpcRequest,
) -> Result<JsonRpcResponse>where
S: Service<RouterRequest, Response = RouterResponse, Error = Infallible> + Clone + Send + 'static,
S::Future: Send,
pub async fn call_single(
&mut self,
req: JsonRpcRequest,
) -> Result<JsonRpcResponse>where
S: Service<RouterRequest, Response = RouterResponse, Error = Infallible> + Clone + Send + 'static,
S::Future: Send,
Process a single JSON-RPC request
Sourcepub async fn call_batch(
&mut self,
requests: Vec<JsonRpcRequest>,
) -> Result<Vec<JsonRpcResponse>>where
S: Service<RouterRequest, Response = RouterResponse, Error = Infallible> + Clone + Send + 'static,
S::Future: Send,
pub async fn call_batch(
&mut self,
requests: Vec<JsonRpcRequest>,
) -> Result<Vec<JsonRpcResponse>>where
S: Service<RouterRequest, Response = RouterResponse, Error = Infallible> + Clone + Send + 'static,
S::Future: Send,
Process a 2025-03-26 batch of JSON-RPC requests concurrently.
The exact revision must have been negotiated by initialize, supplied
by a transport session, or be the sole entry in ProtocolSupport.
Later revisions return an invalid-request error.
Sourcepub async fn call_message(
&mut self,
msg: JsonRpcMessage,
) -> Result<JsonRpcResponseMessage>where
S: Service<RouterRequest, Response = RouterResponse, Error = Infallible> + Clone + Send + 'static,
S::Future: Send,
pub async fn call_message(
&mut self,
msg: JsonRpcMessage,
) -> Result<JsonRpcResponseMessage>where
S: Service<RouterRequest, Response = RouterResponse, Error = Infallible> + Clone + Send + 'static,
S::Future: Send,
Process a JSON-RPC message (single or batch)
Trait Implementations§
Source§impl<S> Clone for JsonRpcService<S>where
S: Clone,
impl<S> Clone for JsonRpcService<S>where
S: Clone,
Source§impl<S> Service<JsonRpcMessage> for JsonRpcService<S>where
S: Service<RouterRequest, Response = RouterResponse, Error = Infallible> + Clone + Send + 'static,
S::Future: Send,
Service implementation for JSON-RPC batch requests
impl<S> Service<JsonRpcMessage> for JsonRpcService<S>where
S: Service<RouterRequest, Response = RouterResponse, Error = Infallible> + Clone + Send + 'static,
S::Future: Send,
Service implementation for JSON-RPC batch requests
Source§type Response = JsonRpcResponseMessage
type Response = JsonRpcResponseMessage
Source§type Future = Pin<Box<dyn Future<Output = Result<<JsonRpcService<S> as Service<JsonRpcMessage>>::Response, <JsonRpcService<S> as Service<JsonRpcMessage>>::Error>> + Send>>
type Future = Pin<Box<dyn Future<Output = Result<<JsonRpcService<S> as Service<JsonRpcMessage>>::Response, <JsonRpcService<S> as Service<JsonRpcMessage>>::Error>> + Send>>
Source§impl<S> Service<JsonRpcRequest> for JsonRpcService<S>where
S: Service<RouterRequest, Response = RouterResponse, Error = Infallible> + Clone + Send + 'static,
S::Future: Send,
impl<S> Service<JsonRpcRequest> for JsonRpcService<S>where
S: Service<RouterRequest, Response = RouterResponse, Error = Infallible> + Clone + Send + 'static,
S::Future: Send,
Source§type Response = JsonRpcResponse
type Response = JsonRpcResponse
Source§type Future = Pin<Box<dyn Future<Output = Result<<JsonRpcService<S> as Service<JsonRpcRequest>>::Response, <JsonRpcService<S> as Service<JsonRpcRequest>>::Error>> + Send>>
type Future = Pin<Box<dyn Future<Output = Result<<JsonRpcService<S> as Service<JsonRpcRequest>>::Response, <JsonRpcService<S> as Service<JsonRpcRequest>>::Error>> + Send>>
Auto Trait Implementations§
impl<S> !RefUnwindSafe for JsonRpcService<S>
impl<S> !UnwindSafe for JsonRpcService<S>
impl<S> Freeze for JsonRpcService<S>where
S: Freeze,
impl<S> Send for JsonRpcService<S>where
S: Send,
impl<S> Sync for JsonRpcService<S>where
S: Sync,
impl<S> Unpin for JsonRpcService<S>where
S: Unpin,
impl<S> UnsafeUnpin for JsonRpcService<S>where
S: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<M, S, Target, Request> MakeService<Target, Request> for M
impl<M, S, Target, Request> MakeService<Target, Request> for M
Source§fn poll_ready(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Result<(), <M as MakeService<Target, Request>>::MakeError>>
fn poll_ready( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<(), <M as MakeService<Target, Request>>::MakeError>>
Poll::Ready when the factory is able to create more services. Read moreSource§fn make_service(
&mut self,
target: Target,
) -> <M as MakeService<Target, Request>>::Future
fn make_service( &mut self, target: Target, ) -> <M as MakeService<Target, Request>>::Future
Source§fn into_service(self) -> IntoService<Self, Request>where
Self: Sized,
fn into_service(self) -> IntoService<Self, Request>where
Self: Sized,
Source§fn as_service(&mut self) -> AsService<'_, Self, Request>where
Self: Sized,
fn as_service(&mut self) -> AsService<'_, Self, Request>where
Self: Sized,
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<S, R> ServiceExt<R> for Swhere
S: Service<R>,
impl<S, R> ServiceExt<R> for Swhere
S: Service<R>,
Source§fn into_make_service(self) -> IntoMakeService<S>
fn into_make_service(self) -> IntoMakeService<S>
MakeService, that is a Service whose
response is another service. Read moreSource§fn into_make_service_with_connect_info<C>(
self,
) -> IntoMakeServiceWithConnectInfo<S, C>
fn into_make_service_with_connect_info<C>( self, ) -> IntoMakeServiceWithConnectInfo<S, C>
MakeService, that will store C’s
associated ConnectInfo in a request extension such that ConnectInfo
can extract it. Read moreSource§fn handle_error<F, T>(self, f: F) -> HandleError<Self, F, T>
fn handle_error<F, T>(self, f: F) -> HandleError<Self, F, T>
HandleError, that will handle errors
by converting them into responses. Read moreSource§impl<T, Request> ServiceExt<Request> for T
impl<T, Request> ServiceExt<Request> for T
Source§fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
Source§fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
Source§fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
Service, calling it with the provided request once it is ready.Source§fn and_then<F>(self, f: F) -> AndThen<Self, F>
fn and_then<F>(self, f: F) -> AndThen<Self, F>
poll_ready method. Read moreSource§fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
poll_ready method. Read moreSource§fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
poll_ready method. Read moreSource§fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
Result<Self::Response, Self::Error>)
to a different value, regardless of whether the future succeeds or
fails. Read more