-
Notifications
You must be signed in to change notification settings - Fork 352
Add ModelContextProtocol.Core package #428
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
add8f86
Add ModelContextProtocol.Hosting package
halter73 8470b8c
Rename ModelContextProtocol and ModelContextProtocol.Hosting packages
Copilot 6cb7b69
Merge remote-tracking branch 'origin/main' into move-deps
halter73 2b66813
Add package overview section to main README with NuGet links and desc…
Copilot c8d2c5d
Merge remote-tracking branch 'origin/main' into move-deps
halter73 23adb94
Add ModelContextProtocol.Core to slnx
halter73 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
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
2 changes: 1 addition & 1 deletion
2
src/ModelContextProtocol.AspNetCore/HttpMcpServerBuilderExtensions.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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
46 changes: 46 additions & 0 deletions
46
src/ModelContextProtocol.Core/ModelContextProtocol.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,46 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<IsPackable>true</IsPackable> | ||
<PackageId>ModelContextProtocol.Core</PackageId> | ||
<Description>Core .NET SDK for the Model Context Protocol (MCP)</Description> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<LangVersion>preview</LangVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(TargetFramework)' != 'netstandard2.0'"> | ||
<IsAotCompatible>true</IsAotCompatible> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\Common\Throw.cs" Link="Throw.cs" /> | ||
</ItemGroup> | ||
|
||
<!-- Dependencies only needed by netstandard2.0 --> | ||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> | ||
<Compile Include="..\Common\Polyfills\**\*.cs" /> | ||
<PackageReference Include="Microsoft.Bcl.Memory" /> | ||
<PackageReference Include="System.Diagnostics.DiagnosticSource" /> | ||
<PackageReference Include="System.Text.Json" /> | ||
<PackageReference Include="System.Threading.Channels" /> | ||
</ItemGroup> | ||
|
||
<!-- Dependencies only needed by netstandard2.0 or net8.0 --> | ||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net8.0'"> | ||
<PackageReference Include="System.IO.Pipelines" /> | ||
</ItemGroup> | ||
|
||
<!-- Dependencies needed by all --> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" /> | ||
<PackageReference Include="System.Net.ServerSentEvents" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="README.md" pack="true" PackagePath="\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,112 @@ | ||
# MCP C# SDK Core | ||
|
||
[](https://www.nuget.org/packages/ModelContextProtocol.Core/absoluteLatest) | ||
|
||
Core .NET SDK for the [Model Context Protocol](https://modelcontextprotocol.io/), enabling .NET applications, services, and libraries to implement and interact with MCP clients and servers. Please visit our [API documentation](https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.html) for more details on available functionality. | ||
|
||
> [!NOTE] | ||
> This project is in preview; breaking changes can be introduced without prior notice. | ||
|
||
## About MCP | ||
|
||
The Model Context Protocol (MCP) is an open protocol that standardizes how applications provide context to Large Language Models (LLMs). It enables secure integration between LLMs and various data sources and tools. | ||
|
||
For more information about MCP: | ||
|
||
- [Official Documentation](https://modelcontextprotocol.io/) | ||
- [Protocol Specification](https://spec.modelcontextprotocol.io/) | ||
- [GitHub Organization](https://github.com/modelcontextprotocol) | ||
|
||
## Installation | ||
|
||
To get started, install the core package from NuGet | ||
|
||
``` | ||
dotnet add package ModelContextProtocol.Core --prerelease | ||
``` | ||
|
||
## Getting Started (Client) | ||
|
||
To get started writing a client, the `McpClientFactory.CreateAsync` method is used to instantiate and connect an `IMcpClient` | ||
to a server. Once you have an `IMcpClient`, you can interact with it, such as to enumerate all available tools and invoke tools. | ||
|
||
```csharp | ||
var clientTransport = new StdioClientTransport(new StdioClientTransportOptions | ||
{ | ||
Name = "Everything", | ||
Command = "npx", | ||
Arguments = ["-y", "@modelcontextprotocol/server-everything"], | ||
}); | ||
|
||
var client = await McpClientFactory.CreateAsync(clientTransport); | ||
|
||
// Print the list of tools available from the server. | ||
foreach (var tool in await client.ListToolsAsync()) | ||
{ | ||
Console.WriteLine($"{tool.Name} ({tool.Description})"); | ||
} | ||
|
||
// Execute a tool (this would normally be driven by LLM tool invocations). | ||
var result = await client.CallToolAsync( | ||
"echo", | ||
new Dictionary<string, object?>() { ["message"] = "Hello MCP!" }, | ||
cancellationToken:CancellationToken.None); | ||
|
||
// echo always returns one and only one text content object | ||
Console.WriteLine(result.Content.First(c => c.Type == "text").Text); | ||
``` | ||
|
||
Clients can connect to any MCP server, not just ones created using this library. The protocol is designed to be server-agnostic, so you can use this library to connect to any compliant server. | ||
|
||
Tools can be easily exposed for immediate use by `IChatClient`s, because `McpClientTool` inherits from `AIFunction`. | ||
|
||
```csharp | ||
// Get available functions. | ||
IList<McpClientTool> tools = await client.ListToolsAsync(); | ||
|
||
// Call the chat client using the tools. | ||
IChatClient chatClient = ...; | ||
var response = await chatClient.GetResponseAsync( | ||
"your prompt here", | ||
new() { Tools = [.. tools] }, | ||
``` | ||
|
||
## Getting Started (Server) | ||
|
||
The core package provides the basic server functionality. Here's an example of creating a simple MCP server without dependency injection: | ||
|
||
```csharp | ||
using ModelContextProtocol.Server; | ||
using System.ComponentModel; | ||
|
||
// Create server options | ||
var serverOptions = new McpServerOptions(); | ||
|
||
// Add tools directly | ||
serverOptions.Capabilities.Tools = new() | ||
{ | ||
ListChanged = true, | ||
ToolCollection = [ | ||
McpServerTool.Create((string message) => $"hello {message}", new() | ||
{ | ||
Name = "echo", | ||
Description = "Echoes the message back to the client." | ||
}) | ||
] | ||
}; | ||
|
||
// Create and run server with stdio transport | ||
var server = new McpServer(serverOptions); | ||
using var stdioTransport = new StdioServerTransport(); | ||
await server.RunAsync(stdioTransport, CancellationToken.None); | ||
``` | ||
|
||
For more advanced scenarios with dependency injection, hosting, and automatic tool discovery, see the `ModelContextProtocol` package. | ||
|
||
## Acknowledgements | ||
|
||
The MCP C# SDK builds upon the excellent work from the [mcpdotnet](https://github.com/ReallyLiri/mcpdotnet) project by [Liri](https://github.com/ReallyLiri). We extend our gratitude for providing a foundational implementation that inspired this SDK. | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License. See the [LICENSE](../../LICENSE) file for details. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.
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.
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.
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.
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.
Love the way diffs work in slnx