pub struct McpHttpHandler {}Implementations§
Source§impl McpHttpHandler
impl McpHttpHandler
Sourcepub fn create_request(
method: Method,
uri: Uri,
headers: HeaderMap,
body: Option<&str>,
) -> Request<&str>
pub fn create_request( method: Method, uri: Uri, headers: HeaderMap, body: Option<&str>, ) -> Request<&str>
Creates a new HTTP request with the given method, URI, headers, and optional body.
§Arguments
method- The HTTP method to use (e.g., GET, POST).uri- The target URI for the request.headers- A map of optional header keys and their corresponding values.body- An optional string slice representing the request body.
§Returns
An http::Request<&str> initialized with the specified method, URI, headers, and body.
If the body is None, an empty string is used as the default.
Source§impl McpHttpHandler
impl McpHttpHandler
Sourcepub async fn handle_sse_connection(
state: Arc<McpAppState>,
sse_message_endpoint: Option<&str>,
) -> TransportServerResult<Response<BoxBody<Bytes, TransportServerError>>>
pub async fn handle_sse_connection( state: Arc<McpAppState>, sse_message_endpoint: Option<&str>, ) -> TransportServerResult<Response<BoxBody<Bytes, TransportServerError>>>
Handles an MCP connection using the SSE (Server-Sent Events) transport.
This function serves as the entry point for initializing and managing a client connection
over SSE when the sse feature is enabled.
§Arguments
state- Shared application state required to manage the MCP session.sse_message_endpoint- Optional message endpoint to override the default SSE route (default:/messages).
§Features
This function is only available when the sse feature is enabled.
Sourcepub async fn handle_sse_message(
request: Request<&str>,
state: Arc<McpAppState>,
) -> TransportServerResult<Response<BoxBody<Bytes, TransportServerError>>>
pub async fn handle_sse_message( request: Request<&str>, state: Arc<McpAppState>, ) -> TransportServerResult<Response<BoxBody<Bytes, TransportServerError>>>
Handles incoming MCP messages from the client after an SSE connection is established.
This function processes a message sent by the client as part of an active SSE session. It:
- Extracts the
sessionIdfrom the request query parameters. - Locates the corresponding session’s transmit channel.
- Forwards the incoming message payload to the MCP transport stream for consumption.
§Arguments
request- The HTTP request containing the message body and query parameters (includingsessionId).state- Shared application state, including access to the session store.
§Returns
TransportServerResult<http::Response<GenericBody>>:- Returns a
202 AcceptedHTTP response if the message is successfully forwarded. - Returns an error if the session ID is missing, invalid, or if any I/O issues occur while processing the message.
- Returns a
§Errors
SessionIdMissing: if thesessionIdquery parameter is not present.SessionIdInvalid: if the session ID does not map to a valid session in the session store.StreamIoError: if an error occurs while writing to the stream.HttpError: if constructing the HTTP response fails.
Sourcepub async fn handle_streamable_http(
request: Request<&str>,
state: Arc<McpAppState>,
) -> TransportServerResult<Response<BoxBody<Bytes, TransportServerError>>>
pub async fn handle_streamable_http( request: Request<&str>, state: Arc<McpAppState>, ) -> TransportServerResult<Response<BoxBody<Bytes, TransportServerError>>>
Handles incoming MCP messages over the StreamableHTTP transport.
It supports GET, POST, and DELETE methods for handling streaming operations, and performs optional
DNS rebinding protection if it is configured.
§Arguments
request- The HTTP request from the client, including method, headers, and optional body.state- Shared application state, including configuration and session management.
§Behavior
- If DNS rebinding protection is enabled via the app state, the function checks the request headers.
If dns protection fails, a
403 Forbiddenresponse is returned. - Dispatches the request to method-specific handlers based on the HTTP method:
GET→handle_http_getPOST→handle_http_postDELETE→handle_http_delete
- Returns
405 Method Not Allowedfor unsupported methods.
§Returns
- A
TransportServerResultwrapping an HTTP response indicating success or failure of the operation.