-
Notifications
You must be signed in to change notification settings - Fork 651
Add IncludeResourceIndicator option for OAuth and DI client sample #1402
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
Open
Varun6578
wants to merge
2
commits into
modelcontextprotocol:main
Choose a base branch
from
Varun6578:fix/oauth-resource-indicator-and-di-sample
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+294
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
samples/DependencyInjectionClient/DependencyInjectionClient.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\src\ModelContextProtocol.Core\ModelContextProtocol.Core.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Hosting" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
| using ModelContextProtocol.Client; | ||
| using ModelContextProtocol.Protocol; | ||
|
|
||
| // This sample demonstrates how to wire up MCP clients with dependency injection | ||
| // using Microsoft.Extensions.Hosting and IServiceCollection. | ||
|
|
||
| var builder = Host.CreateApplicationBuilder(args); | ||
|
|
||
| // Register an MCP client as a singleton. | ||
| // The factory method creates the client with a StdioClientTransport. | ||
| // Replace the command/arguments with your own MCP server (e.g., a Docker container). | ||
| builder.Services.AddSingleton(sp => | ||
| { | ||
| var loggerFactory = sp.GetRequiredService<ILoggerFactory>(); | ||
|
|
||
| var transport = new StdioClientTransport(new() | ||
| { | ||
| Name = "Everything", | ||
| Command = "npx", | ||
| Arguments = ["-y", "@modelcontextprotocol/server-everything"], | ||
| }); | ||
|
|
||
| // McpClient.CreateAsync is async; we block here for DI registration. | ||
| // In production, consider using an IHostedService to initialize async resources. | ||
| return McpClient.CreateAsync(transport, loggerFactory: loggerFactory) | ||
| .GetAwaiter().GetResult(); | ||
| }); | ||
|
|
||
| // Register a hosted service that uses the MCP client. | ||
| builder.Services.AddHostedService<McpWorker>(); | ||
|
|
||
| var host = builder.Build(); | ||
| await host.RunAsync(); | ||
|
|
||
| /// <summary> | ||
| /// A background service that demonstrates using an injected MCP client. | ||
| /// </summary> | ||
| sealed class McpWorker(McpClient mcpClient, ILogger<McpWorker> logger, IHostApplicationLifetime lifetime) : BackgroundService | ||
| { | ||
| protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
| { | ||
| // List available tools from the MCP server. | ||
| var tools = await mcpClient.ListToolsAsync(cancellationToken: stoppingToken); | ||
| logger.LogInformation("Available tools ({Count}):", tools.Count); | ||
| foreach (var tool in tools) | ||
| { | ||
| logger.LogInformation(" {Name}: {Description}", tool.Name, tool.Description); | ||
| } | ||
|
|
||
| // Invoke a tool. | ||
| var result = await mcpClient.CallToolAsync( | ||
| "echo", | ||
| new Dictionary<string, object?> { ["message"] = "Hello from DI!" }, | ||
| cancellationToken: stoppingToken); | ||
|
|
||
| var text = result.Content.OfType<TextContentBlock>().FirstOrDefault()?.Text; | ||
| logger.LogInformation("Echo result: {Result}", text); | ||
|
|
||
| // Shut down after the demo completes. | ||
| lifetime.StopApplication(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Configuring this to false is insecure for MCP apps. We're required by the spec to send the
resourceparameter because of this.See #940 (comment) for more context.
MCP servers are different than most HTTP servers using OAuth authorization code flow that can rely on TLS and limiting redirect URIs to ensure authorization codes and ultimately access tokens don't get sent to the wrong HTTP server (which is the OAuth client in the scenario). MCP uses the authorization code flow to send the authorization code to a non-first-party, non-browser client. Without a resource parameter, neither Entra nor any other OAuth server have any way to validate that the token will be sent to the expected MCP server. And your MCP server has no way to validate that the access token was sent directly to it rather than main-in-the-middled (MITM'd).
The lack of a resource parameter would make it trivial for another MCP server to copy your Protected Resource Metadata document and then MITM your MCP server. All the attacker would need to do is convince someone to enter the wrong URL into their MCP config while protending to be your server. The end-user's browser will happily autofill their credentials as they go to the real OAuth provider's website even though the resulting access token will ultimately be sent to the attackers MCP server first by the non-conforming MCP client that does not send the resource parameter.