Commit graph

154 commits

Author SHA1 Message Date
Stephen Toub
d21d933876
Move notification handler registrations to capabilities (#207) v0.1.0-preview.6
* 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
2025-04-04 13:11:07 -04:00
Stephen Toub
8fcdf95785
Consolidate McpClientException/McpServerException into McpException (#209) 2025-04-03 21:10:57 -04:00
Stephen Halter
d1a3c201a4
Disable response compression for SSE responses (#208) 2025-04-03 17:53:58 -07:00
Eirik Tsarpalis
3b26bf8b1e
Bump package version to 0.1.0-preview.6 (#206) 2025-04-03 20:27:55 +01:00
Esteban Solano Granados
c70dde3feb
[UnitTests] Add more UTs (#139) v0.1.0-preview.5
* + Tests for McpClientExtensions code

* + code coverage for ClientOptions/Capabilities code

---------

Co-authored-by: Esteban Solano <estebanso@microsoft.com>
Co-authored-by: Stephen Toub <stoub@microsoft.com>
2025-04-03 14:18:34 -04:00
Andy Gocke
f652b1a03d
Mark samples as AOT compatible (#184)
* Revert changes to remove APIs

* Apply suggestions from code review

Co-authored-by: Stephen Toub <stoub@microsoft.com>

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
2025-04-03 14:12:25 -04:00
Stephen Toub
c452dc8e26
Reduce dependencies to 8.x versions of packages (#195)
* 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
2025-04-03 14:01:46 -04:00
Eirik Tsarpalis
1c145a5d52
Hardcode the source generator in one remaining location and simplify McpJsonUtilities. (#204) 2025-04-03 13:42:42 -04:00
Stephen Toub
a04cf6a2f5
Expand a few DI-related tests (#201) 2025-04-03 12:48:23 -04:00
Peder Holdgaard Pedersen
4ecb6bc240
Allow override of name and description of McpClientTool (#165) 2025-04-02 14:05:08 -07:00
Sam Spencer
9302466041
Changed the name of the ActivitySource and Metric to clarify that the semantic conventions have not been ratified yet. (#194)
* 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>
2025-04-02 17:02:46 -04:00
Stephen Halter
9b4db350c3
Write "event: message" to SSE response (#192)
* Write "event: message" to SSE response
* Rename McpJsonRpcEndpoint to McpEndpoint
* Remove unused McpServerConfig.Arguments property
2025-04-02 13:26:39 -07:00
Eirik Tsarpalis
f1af251c32
Allow JsonSerializerOptions parameters for user-defined and inputs and hardcode everything else to the source generator. (#182)
* 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
2025-04-02 18:43:54 +01:00
Stephen Toub
4b38d3e71b
Add initial Activity / Metric support (#183) 2025-04-02 11:09:05 -04:00
Stephen Halter
674cb1515c
Make supposedly unreachable code less reachable (#178)
* 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
2025-04-01 22:29:59 -04:00
Tyler Kendrick
25bcb440a0
Extend progress notification support (#163)
* Extend progress notification support

* Address feedback and fix test

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
2025-04-01 11:43:50 -04:00
Stephen Toub
b12d728700
Fix pagination handling in McpServer (#177)
- 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.
2025-04-01 09:54:19 -04:00
Stephen Halter
8dc1f5d096
Configure hosted stdio servers to log to stderr (#173)
* Configure hosted stdio servers to log to stderr

* Fix newline

* Use CreateApplicationBuilder where possible

* Address PR feedback
2025-03-31 22:23:49 -04:00
Stephen Toub
19eed91c53
Enable IsAotCompatible for ModelContextProtocol.AspNetCore (#175) 2025-03-31 22:18:16 -04:00
Aaron Powell
b9bb871787
Fixing WithPromptsFromAssembly (#174)
* Fixing WithPromptsFromAssembly

Was looking for tools

* Fixing test
2025-03-31 20:59:31 -04:00
Stephen Toub
96fa96c1b2
Split StdioClient/ServerTransports into Stdio : Stream (#172) 2025-03-31 18:44:54 -04:00
Stephen Halter
57a526ec30
Put MapMcp in Microsoft.AspNetCore.Builder namespace (#171)
* Still dispose McpSession when MessageProcessingTask throws non-OCE exceptions

* Put MapMcp in icrosoft.AspNetCore.Builder namespace
2025-03-31 17:35:28 -04:00
Eirik Tsarpalis
065e3c6dbb
Bump package version (#169) 2025-03-31 21:26:55 +01:00
Stephen Halter
ce89fa0b50
Fix AspNetCore README (#170)
* Use Cache-Control: no-store

* Fix AspNetCore README

* remove and sort
2025-03-31 16:17:17 -04:00
Eirik Tsarpalis
4c537ef86b
Bump package version (#167) v0.1.0-preview.4 2025-03-31 20:21:00 +01:00
Stephen Halter
973c7c093f
Add ModelContextProtocol.AspNetCore (#160)
* 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>
2025-03-31 12:01:04 -07:00
Jeff Handley
330e526446
Bump version to 0.1.0-preview.3 (#162) v0.1.0-preview.3 2025-03-31 17:38:31 +01:00
Stephen Toub
8fe96bf6ba
Implement cancellation notifications (#146)
* Implement cancellation notifications

* Address PR feedback

* Address feedback
2025-03-31 12:09:47 -04:00
Stephen Toub
5d3fb65b11
Add McpServerPrompt / McpClientPrompt and friends (#126) 2025-03-31 09:54:40 -04:00
Stephen Toub
2c75acded8
Improve progress reporting (#145)
* 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
2025-03-31 09:33:16 -04:00
Stephen Toub
0294c574a6
Fix binding to wrong overload in WithTools (#152) 2025-03-31 06:28:46 -04:00
Aaron Powell
3c0679e92b
Overhaul of Resources (#125)
* 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>
2025-03-30 22:45:03 -04:00
Stephen Halter
2ab9db0878
Refactor transports to help enable graceful shutdown (#142)
- 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>
2025-03-30 17:10:14 -07:00
Stephen Toub
d47c834fa9
Remove Assembly.GetCallingAssembly (#133)
This is always called from M.E.DI. That's not a good name to use here.
2025-03-28 20:46:53 -04:00
Stephen Toub
96dd7dcff3
Add missing Annotations (#138) 2025-03-28 20:46:34 -04:00
Brennan
f55302b449
Fix sample in README (#136) 2025-03-28 16:59:38 -04:00
Stephen Toub
62e92fa154
Add ToolAnnotations support (#124)
* Add ToolAnnotations support

* Address feedback
2025-03-28 11:08:29 -04:00
Stephen Toub
2c1672cc1a
Fix some issues with Native AOT (#130)
* 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.
2025-03-28 09:50:56 -04:00
nevermore
9330774795
complete fix for SampleLlmTool discovery (#102) v0.1.0-preview.2
https://github.com/modelcontextprotocol/csharp-sdk/issues/80

Co-authored-by: Stephen Toub <stoub@microsoft.com>
2025-03-27 18:22:25 -04:00
Jeff Handley
259f11af8a
Set up package publishing for daily, manual, and release builds (#118)
* 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>
2025-03-27 15:08:19 -07:00
Stephen Halter
4a53f6f6d0
List tools from DI once (#115)
* 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
2025-03-27 07:26:14 -04:00
Aaron Powell
3c72c348d6
Adding support for returning collections from tools (#116)
* 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
2025-03-27 00:25:25 -04:00
Aaron Powell
5171ccc3c3
Samples for quickstart docs (#104)
* Server quickstart for docs

* WIP

* Client quickstart for docs

* Code review feedback

* Fixing anthropic SDK and improving the prompting

* Little cleanup

* Code review feedback
2025-03-26 22:30:19 -04:00
Stephen Halter
8be681543d
Use different logger categories for client and server in McpJsonRpcEndpoint (#110)
* Use different logger categories for client and server in McpJsonRpcEndpoint

* Address PR feedback
2025-03-26 22:18:16 -04:00
Eduard Stanculet
d1217c57af
Configurable messages endpoint for SSE transport (#109) 2025-03-26 21:05:49 -04:00
Stephen Toub
c77e78394d Fix spacing 2025-03-26 16:05:00 -04:00
Stephen Halter
95a017a03a
Make options types fully mutable (#107) 2025-03-26 10:39:49 -07:00
Stephen Toub
b61cdef994
Add [McpServerTool] support for instance methods (#100)
* 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
2025-03-26 09:34:30 -04:00
Aaron Powell
6fa1443a6c
Adding a base class for request params that exposes the progressToken (#99)
* Adding a base class for request params that exposes the progressToken

This aligns more with the type structure see in the other SDK's (eg: https://github.com/modelcontextprotocol/python-sdk/blob/main/src/mcp/types.py#L41) while also ensuring they are exposing the JSONRPCRequest (https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json#L771-L806) data.

Storing progressToken as an object as it could be an int or string (per https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json#L1268-L1274).

Fixes #67

* Using usings

* Apply suggestions from code review

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
2025-03-25 23:40:48 -04:00
Stephen Halter
a9175f0917
Add gitattributes (#98) 2025-03-25 18:57:01 -07:00