How can I pass user-specific AUTH headers to MCP without recreating the Kernel? #12583
Unanswered
shlomico-tr
asked this question in
Q&A
Replies: 3 comments
-
|
Any answer? |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Adding @markwallace-microsoft to help with the .NET side of things. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Updates? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a question about the TOOLS in the Kernel—especially MCP.
Is there a way to define custom headers that can be changed according to the signed-in user?
The only way I’ve found so far is to clone or create a brand-new Kernel, which is inefficient with MCP because it has to reload the tools and that takes time we’d rather save.
In practice, I have an AUTH header that I want to pass—using a different value for each
My code:
// Configure MCP client (with per-user auth header) and add plugin to a dedicated kernel await using var mcpClient = await McpClientFactory.CreateAsync( clientTransport: new SseClientTransport(new() { Endpoint = new Uri("https://my-mcp-instance.workers.dev/sse"), // <-- replace with your endpoint AdditionalHeaders = new Dictionary { { "Authorization", $"Bearer {userAuthToken}" } // <-- user-specific token } })).ConfigureAwait(false); // Discover available tools var tools = await mcpClient.ListToolsAsync().ConfigureAwait(false); foreach (var tool in tools) { logger.LogInformation($"{tool.Name}: {tool.Description}"); } // Add plugin functions to the kernel #pragma warning disable SKEXP0001 // Type is for evaluation purposes only and may change customKernel.Plugins.AddFromFunctions( "CustomPlugin", tools.Select(f => f.AsKernelFunction())); #pragma warning restore SKEXP0001 // Register the kernel as a keyed service builder.Services.AddKeyedSingleton("custom", customKernel);Beta Was this translation helpful? Give feedback.
All reactions