Commit graph

128 commits

Author SHA1 Message Date
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
Tim Heuer
4b70bc7e16
Add package installation (#101)
Add package installation snippet
2025-03-25 21:35:04 -04:00
Stephen Toub
caca2ae61d
Overhaul tool handling (#89)
* Overhaul tool handling

- Renames [McpTool{Type}] to [McpServerTool{Type}], in order to distinguish representations of tools on the server from tools on the client.
- Enables [McpServerTool] methods to have arguments injected from DI, as well as a specific set of types from the implementation directly, like IMcpServer.
- Renames WithTools to WithToolsFromAssembly.
- All of the WithToolsXx methods now publish each individual McpServerTool into DI; other code can do so as well. The options setup code gathers all of the tools from DI and combines them into a collection which is then stored in McpServerOptions and used to construct the server.
- The server tools specified via DI as well as manually-provided handlers, using CallToolHandler as a fallback in case the requested tool doesn't exist in the tools collection, and ListToolsHandler augments the information for whatever tools exist.
- The tools are stored in McpServerOptions in a new McpServerToolCollection type, which is a thread-safe container that exposes add/removal notification. Adding/removing tools triggers a change notification that in turn sends a notification to the client about a tools update.
- The ServerOptions are exposed from the server instance.
- Removed cursor-based APIs from McpClientExtensions.
- Changed McpClientExtensions APIs to return `Task<IList<...>` rather than `IAsyncEnumerable<...>`.

I'm sure this will need to evolve further before we're "done", but it's a significant improvement from where we are now. One area I'm not super happy with is the notifying collection; that might work ok for a stdio server, where you can grab the created collection from the server's options and mutate it, but I don't know how well that's going to work for sse servers.

* Make McpServerTool{Collection} extensible

* Fix README.md example

* Add notifications test

* Address feedback
2025-03-25 15:32:46 -04:00
Eirik Tsarpalis
aafa9d929d
Add links to API docs from README.md (#96) 2025-03-25 15:55:57 +00:00
Den Delimarsky
2972fd715a
Update documentation configuration (#86)
* Docs configuration

* Update docs/index.md

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>

* Update the configuration

* Update docs/docfx.json

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>

* Update config

* Update icons, use Makefile, remove warnings

* Use a link that passes markdown link validation

* remove working directory

---------

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
2025-03-25 15:43:14 +00:00
Aaron Powell
1f8ebee742
Adding code coverage to tests in workflow (#75)
* Adding code coverage to tests in workflow

This is using the pattern from the Aspire Community Toolkit which will publish coverage results to the GitHub Actions summary, as well as a comment on the PR (if the job was run by someone with permissions to write comments).

Also added the GitHubActionsTestLogger so that it writes a nicer log out to the run.

Fixes #11

* Feedback from code review

* Slight change to structure

* Bad indentation

* Ensuring .NET SDK is installed

* Disabling fail-fast so that jobs aren't terminated if one fails

* Forgot to include coverlet.collector

* Update Directory.Packages.props

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
2025-03-25 15:34:55 +00:00
Mike Kistler
ed079b9711
Add acknowledgements to README (#94)
* Add acknowledgements to README

* Apply suggestions from PR review.

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

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
2025-03-25 10:31:20 -04:00
Stephen Halter
367f265516
Avoid race starting SseResponseStreamTransport (#91) 2025-03-25 08:54:57 -04:00
Stephen Halter
60ea96a97c
Add XunitLoggerProvider (#81)
* Add XunitLoggerProvider

* Avoid logging to stale ITestOutputHelper in fixture disposal

* Use LoggedTest base class everywhere an ILoggerFactory is used
2025-03-25 08:53:55 -04:00
Stephen Toub
729e688b25
Make client config optional to McpClientFactory.CreateAsync (#65)
* Make client config optional to McpClientFactory.CreateAsync

Unless you're specifying capabilities (sampling/rooting) or need a specific client name/version, the client information can be inferred from the assembly, simplifying the getting-started experience.

* Cache default options

* Update src/ModelContextProtocol/Client/McpClientFactory.cs

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>

---------

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
2025-03-24 11:01:13 -04:00
Brandon Williams
36d5019de0
Fix stdio encoding issue: Enforce explicit UTF-8 for correct Unicode handling (#73)
* fix: Enforce UTF-8 encoding for stdio transport

This change replaces the default system encoding with an explicit UTF8Encoding (without BOM)
for both client and server transports. This ensures proper handling of Unicode characters,
including Chinese characters and emoji.

- Use UTF8Encoding explicitly for StreamReader and StreamWriter.
- Add tests for Chinese characters ("上下文伺服器") and emoji (🔍🚀👍) to confirm the fix.

Fixes #35.

* Remove relaxed JSON encoder

* Address PR feedback

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
2025-03-24 09:55:10 -04:00
Zae Chao
96ba2fa076
Fix enum serialization (#61)
* Fix enum serialization

* Add tests for enum serialization

* Add missing enum serialization configuration.

* Ensure enums serialize as expected using defaults

---------

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
2025-03-24 12:51:24 +00:00
ConanYao
1a5732b158
docs: add McpServerOptionsSetup code comment (#77) 2025-03-23 22:30:39 -04:00
Aaron Powell
c3988737c9
Adding merge files to gitignore (#78) 2025-03-23 22:26:34 -04:00
Weihan Li
a97a74f330
docs: update McpToolAttribute name comment (#63) 2025-03-23 11:51:48 -04:00
Jared Parsons
e76dbb5372
Fix private reflection (#60)
* Fix private reflection

Two changes in this PR

- Remove private reflection from one of the tests
- Move to use `TaskCompletionSource` (feedback from previous PR)

* fix

* spelling
2025-03-22 12:09:43 -04:00
Jared Parsons
d18da9a08d
Remove reflection from Connect test (#57)
* Remove reflection from Connect test

This removes the reflection from the double connection test and replaces it with a
`MockHttpHandler` to let the connection actually succeed and then test the
redundant `ConnectAsync` call.

partial fix for #48

* remove unneeded using

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
2025-03-22 11:56:05 +02:00
Stephen Halter
08e7da100f
Run integration test SSE server in process (#56)
* Run integration test SSE server in process

* Handle expected OCE disposing fixture
2025-03-21 22:18:55 -04:00
Stephen Toub
54e60fe7a2
Update README.MD (#58) 2025-03-21 22:14:49 -04:00
Peder Holdgaard Pedersen
10c712a12f
Logging Capability (#53)
* Logging Capability

* Readability

* Redundant using

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
2025-03-21 17:50:41 -04:00
Jeff Handley
4d61007c11
Set the NuGet package copyright text (#54) v0.1.0-preview.1.25171.12
* Set the NuGet package copyright text

* Temporarily set authors/copyright to Microsoft for publishing
2025-03-21 14:31:27 -07:00
Stephen Halter
1b1c7fa8ca
Move the SseResponseStreamTransport out of sample (#47) 2025-03-21 13:47:29 -07:00
Jeff Handley
e1a5828036
Remove Nerdbank.GitVersioning and set version to 0.1.0-preview (#52) 2025-03-21 13:09:58 -07:00
Jeff Handley
0309dfc447
Add a release workflow for package build/publishing (#51)
* Add a release workflow for package build/publishing

* Use Windows. Set the fetch depth. Fix build/test/pack for output folder. Upload the artifact.
2025-03-21 12:46:45 -07:00
Jonathan Dick
0cbcad7447
Refactor Resource/ResourceTemplate to subclass Annotated (#50)
The `Resource` and `ResourceTemplate` types should derive from a base `Annotated` type having the `Annotations` property as per the [spec](https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.ts#L821)

This small change makes the C# types match the schema more closely.

Also renamed Resources.cs to Resource.cs to reflect the type name.
2025-03-21 15:10:27 -04:00
Jonathan Dick
fe6b401014
Add initial support for Resource Templates (#18)
* Add initial support for Resource Templates

* Make ListResourceTemplatesHandler not required

The server was throwing if this handler wasn't specified, and it should probably be optional anyway.

* Remove extra line

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

* Fix spaces

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

* Fix spaces

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

* Add and use a default ListResourceTemplatesHandler

If no ListResourceTemplatesHandler is specified, use a default one that returns an empty result.

* Fix spaces

* Spaces

* Add Client extensions for ListResourceTemplates

* Integration Test for Client ListResourceTemplates

* Fix comment

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

* Make Defaults internal

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

* Simplify test call

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

* Make delegate static

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

* Fix test comment

* Spaces

* Fix xml doc

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

* Make default handler inline

* Spaces

Co-authored-by: Stephen Halter <halter73@gmail.com>

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
Co-authored-by: Stephen Halter <halter73@gmail.com>
2025-03-21 14:09:45 -04:00
Eirik Tsarpalis
50cc48830e
Add strong naming (#49) 2025-03-21 17:51:05 +00:00