* Move notification handler registrations to capabilities
Currently request handlers are set on the capability objects, but notification handlers are set after construction via an AddNotificationHandler method on the IMcpEndpoint interface. This moves handler specification to be at construction as well. This makes it more consistent with request handlers, simplifies the IMcpEndpoint interface to just be about message sending, and avoids a concurrency bug that could occur if someone tried to add a handler while the endpoint was processing notifications.
* Address more feedback and further cleanup
* Reduce dependencies to 8.x versions of packages
When building for netstandard2.0 or net8.0, the ModelContextProtocol library now only depends on 8.x versions of nuget packages, rather than using the newer 9.x versions. The exception to this are packages that don't have 8.x versions.
This also introduces a net9.0 tfm. When building for net9.0, it'll use 9.x versions of packages.
The PR temporarily removes the Microsoft.Extensions.AI.nupkg reference (the Microsoft.Extensions.AI.Abstractions reference is still very much there). The latest Microsoft.Extensions.AI package on nuget has a System.Text.Json 9.x reference, but the next build to be published drops that down to 8.x as well. Once that build is published, the dependency will be added back, and temporary polyfills (in particular TemporaryAIFunctionFactory) will be deleted.
* Fix StdioClientSessionTransport shutdown
StreamClientSessionTransport's ReadMessageAsync method does await CleanupAsync in its finally. CleanupAsync awaits _readTask. _readTask _is_ ReadMessageAsync. So it's waiting for itself to complete, which will then always timeout. We may want to refactor how this works more in the future, but for now, make sure it doesn't wait for itself.
* Fix dangling requests when server unexpectedly shuts down
If the client makes a request to the server and while waiting for the response the server shuts down, the client hangs. Make sure that when the client's message loops ends due to the server's output it's reading from ends, any pending requests are notified.
* Add net9.0 testing
* Avoid docker port conflicts from test suites running concurrently
* Changed the name of the ActivitySource and Metric to clarify that the semantic conventions have not been ratified yet.
* Update src/ModelContextProtocol/Diagnostics.cs
---------
Co-authored-by: Sam Spencer <samsp@microsoft.com>
Co-authored-by: Stephen Toub <stoub@microsoft.com>
* Allow user-defined JsonSerializerOptions for user-defined and inputs and hardcode everything else to the source generator.
* Add validation for user-defined JsonSerializerOptions.
* Remove out of date comment.
* Address feedback and add tests
* Move WriteJsonRpcMessageToBuffer to method
* Fix indentation in McpClient
* Make supposedly unreachable code less reachable
* Guard against multiple RunAsync calls
* Move RunAsync inside of Try in MapMcp
- We were adding tools/prompts from the collections on every request. If multiple requests came in with different cursors, we'd re-add the same tools each time.
- We were defeating the purpose of pagination by doing all of the aggregation in the server. If a custom handler returns a paginated result, we should instead propagate that back to the client, who can choose to get more results when needed.
* Add ModelContextProtocol.AspNetCore
- Remove IServerTransport and HttpListenerSseServerTransport
- Replace HttpListenerSseServerTransport usage in tests with ASP.NET Core.
Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
Co-authored-by: Jeff Handley <jeffhandley@users.noreply.github.com>
* Improve progress reporting
- Enable injecting an `IProgress<>` into a tool call to report progress when the client has supplied a progress token
- Add a custom type for ProgressToken and ProgressNotification to map to the schema
- Simplify RequestId to match ProgressToken's shape
- Serialize StdioClientTransport's SendMessageAsync implementation
- Add strongly-typed names for all request methods
- Change all request IDs created by McpJsonRpcEndpoint to include a guid for that endpoint
* Address PR feedback
* Supporting resource templates with read resource callback
Fixes#123
* Aligning the resource data structures with the 2024-11-05 schema
- Introducing separate classes for Text and Blob ResourceContents
- Custom JSON converter to allow those types to be returned, making it easier to follow the type system against the spec
- Updated tests
* Apply suggestions from code review
Co-authored-by: Stephen Toub <stoub@microsoft.com>
Co-authored-by: Peder Holdgaard Pedersen <127606677+PederHP@users.noreply.github.com>
* Putting files back where they belong
* Adding an internal constructor to prevent overloads
* Addressing feedback
* Apply suggestions from code review
Co-authored-by: Stephen Toub <stoub@microsoft.com>
* Update src/ModelContextProtocol/Protocol/Types/ResourceContents.cs
* Addressing feedback
---------
Co-authored-by: Stephen Toub <stoub@microsoft.com>
Co-authored-by: Peder Holdgaard Pedersen <127606677+PederHP@users.noreply.github.com>
- This also paves the way for better multi-session support
- We should definitely rethink names for the transport API
- For now, I kept the names similar as possible, so we can
focus on the API shape
Co-authored-by: Stephen Toub <stoub@microsoft.com>
* Fix some issues with Native AOT
- An anonymous type was being used in McpClient for the initialize result message. Switched to use the type for that purpose that already existed.
- A bunch of the request params / result types were not in the source context or referenced from types that were or from custom converters. Added all the request/result types to the context.
- Improved the warning message for WithTools.
* Set up a root artifacts folder for all build/test/pack output
Update CI TestResults folder
* Update package Authors and Copyright
* Lower-case extension on README.md
* Move package props to src folder. Use Version prefix and suffix props.
* Set up package publishing for daily, manual, and release builds
* Only run publishing jobs to the modelcontextprotocol/csharp-sdk repo
* Fix condition on docs workflow
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update preview wording in README to apply to the package too
* Verify packaging succeeds in the CI workflow
* Allow a version suffix override in release workflow.
* De-dupe the README between the repo and the NuGet package
* New CI work
* Do not reuse the CI workflow for releases so we can build without tests.
* Add a release process doc
* Fix bug in publishing; needs checkout step. Update comment for publish.
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* List tools from DI once
- Follow best practice to not mutate options outside of IConfigureOptions
- Long-term, we should move the repeated work from SetToolsHandler into
an IPostConfigureOptions service or IMcpServerConnectionFactory
* Add Can_Create_Multiple_Servers_From_Options_And_List_Registered_Tools
* Flip stdin and stdout in StreamClientTransport
* s/McpToolType/McpServerToolType
* Clarify stdin and stdout refers to server process in StreamClientTransport
* fixup
* Adding support for returning collections from tools
This will mean we can return multiple AIContent objects from a tool, such as a mixed text/image set
Contributes to #68
* Code review feedback
* Adding tests and some more cases
* More cases in switch and tests
* Add [McpServerTool] support for instance methods
By popular demand, enables [McpServerTool] on instance methods. The instance is constructed per invocation, using ActivatorUtilities.CreateInstance when possible so that constructor arguments are satisfied from DI.
* Add disposal tests