-
Notifications
You must be signed in to change notification settings - Fork 567
Add back EverythingServer for stdio #950
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
asklar
wants to merge
4
commits into
modelcontextprotocol:main
Choose a base branch
from
asklar:copilot/add-stdio-option-for-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.
+478
−195
Open
Changes from all commits
Commits
Show all changes
4 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
13 changes: 13 additions & 0 deletions
13
samples/EverythingServer.Core/EverythingServer.Core.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,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\src\ModelContextProtocol\ModelContextProtocol.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
149 changes: 149 additions & 0 deletions
149
samples/EverythingServer.Core/EverythingServerExtensions.cs
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,149 @@ | ||
| using EverythingServer.Core.Prompts; | ||
| using EverythingServer.Core.Resources; | ||
| using EverythingServer.Core.Tools; | ||
| using Microsoft.Extensions.AI; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using ModelContextProtocol; | ||
| using ModelContextProtocol.Protocol; | ||
| using ModelContextProtocol.Server; | ||
| using System.Collections.Concurrent; | ||
|
|
||
| namespace EverythingServer.Core; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for configuring the EverythingServer MCP handlers. | ||
| /// </summary> | ||
| public static class EverythingServerExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds all the tools, prompts, resources, and handlers for the EverythingServer. | ||
| /// </summary> | ||
| /// <param name="builder">The MCP server builder.</param> | ||
| /// <param name="subscriptions">The subscriptions dictionary to use for managing resource subscriptions.</param> | ||
| /// <returns>The MCP server builder for chaining.</returns> | ||
| public static IMcpServerBuilder AddEverythingMcpHandlers( | ||
| this IMcpServerBuilder builder, | ||
| ConcurrentDictionary<string, ConcurrentDictionary<string, byte>> subscriptions) | ||
| { | ||
| return builder | ||
| .WithTools<AddTool>() | ||
| .WithTools<AnnotatedMessageTool>() | ||
| .WithTools<EchoTool>() | ||
| .WithTools<LongRunningTool>() | ||
| .WithTools<PrintEnvTool>() | ||
| .WithTools<SampleLlmTool>() | ||
| .WithTools<TinyImageTool>() | ||
| .WithPrompts<ComplexPromptType>() | ||
| .WithPrompts<SimplePromptType>() | ||
| .WithResources<SimpleResourceType>() | ||
| .WithSubscribeToResourcesHandler(async (ctx, ct) => | ||
| { | ||
| var sessionId = ctx.Server.SessionId ?? "stdio"; | ||
|
|
||
| if (!subscriptions.TryGetValue(sessionId, out var sessionSubscriptions)) | ||
| { | ||
| sessionSubscriptions = new ConcurrentDictionary<string, byte>(); | ||
| subscriptions[sessionId] = sessionSubscriptions; | ||
| } | ||
|
|
||
| if (ctx.Params?.Uri is { } uri) | ||
| { | ||
| sessionSubscriptions.TryAdd(uri, 0); | ||
|
|
||
| await ctx.Server.SampleAsync([ | ||
| new ChatMessage(ChatRole.System, "You are a helpful test server"), | ||
| new ChatMessage(ChatRole.User, $"Resource {uri}, context: A new subscription was started"), | ||
| ], | ||
| options: new ChatOptions | ||
| { | ||
| MaxOutputTokens = 100, | ||
| Temperature = 0.7f, | ||
| }, | ||
| cancellationToken: ct); | ||
| } | ||
|
|
||
| return new EmptyResult(); | ||
| }) | ||
| .WithUnsubscribeFromResourcesHandler(async (ctx, ct) => | ||
| { | ||
| var sessionId = ctx.Server.SessionId ?? "stdio"; | ||
|
|
||
| if (subscriptions.TryGetValue(sessionId, out var sessionSubscriptions)) | ||
| { | ||
| if (ctx.Params?.Uri is { } uri) | ||
| { | ||
| sessionSubscriptions.TryRemove(uri, out _); | ||
| } | ||
| } | ||
| return new EmptyResult(); | ||
| }) | ||
| .WithCompleteHandler(async (ctx, ct) => | ||
| { | ||
| var exampleCompletions = new Dictionary<string, IEnumerable<string>> | ||
| { | ||
| { "style", ["casual", "formal", "technical", "friendly"] }, | ||
| { "temperature", ["0", "0.5", "0.7", "1.0"] }, | ||
| { "resourceId", ["1", "2", "3", "4", "5"] } | ||
| }; | ||
|
|
||
| if (ctx.Params is not { } @params) | ||
| { | ||
| throw new NotSupportedException($"Params are required."); | ||
| } | ||
|
|
||
| var @ref = @params.Ref; | ||
| var argument = @params.Argument; | ||
|
|
||
| if (@ref is ResourceTemplateReference rtr) | ||
| { | ||
| var resourceId = rtr.Uri?.Split("/").Last(); | ||
|
|
||
| if (resourceId is null) | ||
| { | ||
| return new CompleteResult(); | ||
| } | ||
|
|
||
| var values = exampleCompletions["resourceId"].Where(id => id.StartsWith(argument.Value)); | ||
|
|
||
| return new CompleteResult | ||
| { | ||
| Completion = new Completion { Values = [.. values], HasMore = false, Total = values.Count() } | ||
| }; | ||
| } | ||
|
|
||
| if (@ref is PromptReference pr) | ||
| { | ||
| if (!exampleCompletions.TryGetValue(argument.Name, out IEnumerable<string>? value)) | ||
| { | ||
| throw new NotSupportedException($"Unknown argument name: {argument.Name}"); | ||
| } | ||
|
|
||
| var values = value.Where(value => value.StartsWith(argument.Value)); | ||
| return new CompleteResult | ||
| { | ||
| Completion = new Completion { Values = [.. values], HasMore = false, Total = values.Count() } | ||
| }; | ||
| } | ||
|
|
||
| throw new NotSupportedException($"Unknown reference type: {@ref.Type}"); | ||
| }) | ||
| .WithSetLoggingLevelHandler(async (ctx, ct) => | ||
| { | ||
| if (ctx.Params?.Level is null) | ||
| { | ||
| throw new McpProtocolException("Missing required argument 'level'", McpErrorCode.InvalidParams); | ||
| } | ||
|
|
||
| // The SDK updates the LoggingLevel field of the IMcpServer | ||
|
|
||
| await ctx.Server.SendNotificationAsync("notifications/message", new | ||
| { | ||
| Level = "debug", | ||
| Logger = "test-server", | ||
| Data = $"Logging level set to {ctx.Params.Level}", | ||
| }, cancellationToken: ct); | ||
|
|
||
| return new EmptyResult(); | ||
| }); | ||
| } | ||
| } |
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
4 changes: 2 additions & 2 deletions
4
...ythingServer/Prompts/ComplexPromptType.cs → ...gServer.Core/Prompts/ComplexPromptType.cs
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
2 changes: 1 addition & 1 deletion
2
...rythingServer/Prompts/SimplePromptType.cs → ...ngServer.Core/Prompts/SimplePromptType.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # EverythingServer.Core | ||
|
|
||
| This is the core library containing all the shared MCP server handlers (tools, prompts, resources) used by both the HTTP and stdio implementations of the EverythingServer sample. | ||
|
|
||
| ## What's Inside | ||
|
|
||
| This library contains: | ||
|
|
||
| - **Tools**: Various example tools demonstrating different MCP capabilities | ||
| - `AddTool`: Simple addition operation | ||
| - `AnnotatedMessageTool`: Returns annotated messages | ||
| - `EchoTool`: Echoes input back to the client | ||
| - `LongRunningTool`: Demonstrates long-running operations with progress reporting | ||
| - `PrintEnvTool`: Prints environment variables | ||
| - `SampleLlmTool`: Example LLM sampling integration | ||
| - `TinyImageTool`: Returns image content | ||
|
|
||
| - **Prompts**: Example prompts showing argument handling | ||
| - `SimplePromptType`: Basic prompt example | ||
| - `ComplexPromptType`: Prompt with multiple arguments | ||
|
|
||
| - **Resources**: Example resources with subscriptions | ||
| - `SimpleResourceType`: Dynamic resource with URI template matching | ||
|
|
||
| - **Background Services**: For managing subscriptions and logging | ||
| - `SubscriptionMessageSender`: Sends periodic updates to subscribed resources | ||
| - `LoggingUpdateMessageSender`: Sends periodic logging messages at different levels | ||
|
|
||
| - **Extension Method**: `AddEverythingMcpHandlers` to configure all handlers in one call | ||
|
|
||
| ## Usage | ||
|
|
||
| Reference this project from your MCP server implementation (HTTP or stdio) and call the extension method: | ||
|
|
||
| ```csharp | ||
| builder.Services | ||
| .AddMcpServer() | ||
| .WithHttpTransport() // or WithStdioServerTransport() | ||
| .AddEverythingMcpHandlers(subscriptions); | ||
| ``` | ||
|
|
||
| See the `EverythingServer.Http` and `EverythingServer.Stdio` projects for complete examples. |
2 changes: 1 addition & 1 deletion
2
...les/EverythingServer/ResourceGenerator.cs → ...verythingServer.Core/ResourceGenerator.cs
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
5 changes: 4 additions & 1 deletion
5
...ythingServer/SubscriptionMessageSender.cs → ...gServer.Core/SubscriptionMessageSender.cs
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
2 changes: 1 addition & 1 deletion
2
samples/EverythingServer/Tools/AddTool.cs → ...es/EverythingServer.Core/Tools/AddTool.cs
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
2 changes: 1 addition & 1 deletion
2
samples/EverythingServer/Tools/EchoTool.cs → ...s/EverythingServer.Core/Tools/EchoTool.cs
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
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
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
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.
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.
@mikekistler, I think you were interested in scenarios where a single server could be used either via stdio or http? IIRC, should we do that in this one rather than having different projects?
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.
I think both patterns are valid and it's useful to illustrate how the core MCP logic can be implemented in a class library and used in either a STDIO or sHTTP server project.