-
Notifications
You must be signed in to change notification settings - Fork 162
[Docs/Help/Feature Request]: Getting authentication details in tool calls #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Some related issues/prs: Here's also an example of me trying to pull the Axum #[tool(tool_box)]
impl ServerHandler for Counter {
// Override default
fn ping(
&self,
context: RequestContext<RoleServer>,
) -> impl Future<Output =std::result::Result<(), McpError>> + Send + '_ {
let ext = &context.meta.0;
tracing::info!("Ping received; Extension {:?} Identity: {:?}", ext, context.extensions.get::<Identity>());
tracing::info!("Found context {:?}", context);
std::future::ready(Ok(()))
}
} The logs for that call look something like this:
|
We didn't clone the extensions from axum into the extensions in rmcp request. So maybe we should clone the extension into mcp request's extension, I will add a patch on this. update: I'd like extract the axum common part into one file, after axum streamable http transport being merged |
Sounds good - thanks a ton @4t145 |
#150 is merged, you can check this example https://github.com/modelcontextprotocol/rust-sdk/pull/163/files#diff-e8a7b3352088986e9a3c5b83cfea597a505d62b2881bb6f0b6dbc07ca6518e9d to see if this pr solved your problem |
@4t145 Seems that we still don't have access to the headers inside tool calls while using the tool macro, right? |
@sahra-karakoc You can get what you want by implementing |
@wolf-sigma @sahra-karakoc exposed the extensions in #199, you can get it by extract |
Disclaimer
I haven't tried using the recent OAuth samples (my source isn't 100% setup to support it yet), but I'm pretty sure this issue applies based on analyzing it. I also did not try implementing a custom transport.
Goal
Get authentication details (specifically the authenticated identity) visible to my tool methods.
What I've done
My source supports API keys - just straight auth, no token exchange or the like. Using the MCP inspector, I've set up the connection with Bearer tokens with the key. I've successfully implemented an Axum middleware that handles the Authentication, returning to the server when it fails (example below).
The problem
Due to the abstractions between Axum and the rest of the MCP framework, there doesn't appear to be a clean way to get any details of the middleware (or anything else from the router for that matter) to the MCP server.
My approach was to assign an
Identity
object to the MCP toolbox struct and assign it "at some point". But this was just my initial thought. I don't care as long as I can get it from the tool calls.Ex:
Since there's no obvious way when constructing the service, I tried assigning it during initialization. For the calls in
ServerHandler
that have it, thecontext
parameter doesn't expose - publicly or privately - info about the AxumRouter
. I tried using theExtensions
from the SDK (those available in context for some of the calls) but I couldn't find a way to set them from the Axum "side".The question
Am I missing something obvious?
If not, I think this would be a critical feature. I'm happy to implement it if there's consensus on how (or if not, I can propose a solution).
Example code that sets Axum extensions
I can provide a full example if it helps.
The text was updated successfully, but these errors were encountered: