pub struct Extension<T>(pub T);Expand description
Extract typed data from router extensions.
This extractor retrieves data that was added to the router via
crate::McpRouter::with_state() or crate::McpRouter::with_extension(), or
inserted by middleware into the request context’s extensions.
§Example
use std::sync::Arc;
use tower_mcp::{McpRouter, ToolBuilder, CallToolResult};
use tower_mcp::extract::{Extension, Json};
use schemars::JsonSchema;
use serde::Deserialize;
#[derive(Clone)]
struct DatabasePool {
url: String,
}
#[derive(Deserialize, JsonSchema)]
struct QueryInput {
sql: String,
}
let pool = Arc::new(DatabasePool { url: "postgres://...".into() });
let tool = ToolBuilder::new("query")
.description("Run a query")
.extractor_handler_typed::<_, _, _, QueryInput>(
(),
|Extension(db): Extension<Arc<DatabasePool>>, Json(input): Json<QueryInput>| async move {
Ok(CallToolResult::text(format!("Query on {}: {}", db.url, input.sql)))
},
)
.build()
.unwrap();
let router = McpRouter::new()
.with_state(pool)
.tool(tool);§Rejection
Returns an ExtensionRejection if the requested type is not found in the extensions.
The rejection contains the type name of the missing extension.
Tuple Fields§
§0: TTrait Implementations§
Source§impl<S, T> FromToolRequest<S> for Extension<T>
impl<S, T> FromToolRequest<S> for Extension<T>
Source§type Rejection = ExtensionRejection
type Rejection = ExtensionRejection
The rejection type returned when extraction fails.
Source§fn from_tool_request(
ctx: &RequestContext,
_state: &S,
_args: &Value,
) -> Result<Self, Self::Rejection>
fn from_tool_request( ctx: &RequestContext, _state: &S, _args: &Value, ) -> Result<Self, Self::Rejection>
Extract this type from the tool request. Read more
Auto Trait Implementations§
impl<T> Freeze for Extension<T>where
T: Freeze,
impl<T> RefUnwindSafe for Extension<T>where
T: RefUnwindSafe,
impl<T> Send for Extension<T>where
T: Send,
impl<T> Sync for Extension<T>where
T: Sync,
impl<T> Unpin for Extension<T>where
T: Unpin,
impl<T> UnwindSafe for Extension<T>where
T: UnwindSafe,
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
Mutably borrows from an owned value. Read more