Commit graph csharp-sdk/tests
Author SHA1 Message Date
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
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
Stephen Halter
a9175f0917
Add gitattributes (#98) 2025-03-25 18:57:01 -07: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
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
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
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
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
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
0be5963f8c
Remove InternalsVisibleTo from main project. (#46) 2025-03-21 17:35:19 +00:00
Stephen Toub
33b9582199
Address some static analysis and TODOs (#12) 2025-03-20 23:09:38 -04:00
Stephen Toub
3de19d38e4
Change type of Tool.InputSchema to be JsonElement (#4)
* Change type of Tool.InputSchema to be JsonElement

* Add schema validation in the InputSchema property setter.

* Simplify AIFunction tool adapter.

---------

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
2025-03-20 19:16:41 +02:00
Stephen Toub
27ba8b1b6a
Upgrade to xunit.v3 (#8)
* Upgrade to xunit.v3

* Use Assert.Skip where applicable.

---------

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
2025-03-20 16:19:35 +02:00
Stephen Toub
6b8c288029
Make McpJsonUtilities public and remove IVT to sample (#5)
Also adds in the same encoder used by AIJsonUtilities.
2025-03-20 16:00:23 +02:00
Stephen Toub
555933a06e
Ensure all test client instances are cleaned up (#3) 2025-03-19 17:27:49 -04:00
Stephen Toub
fd358b437d Fix GetAIFunctionsAsync test to be more deterministic 2025-03-19 16:20:45 -04:00
Eirik Tsarpalis
2e1e20645f Add Directory.Build.props, apply CPM, use NB.GitVersioning, update package publishing 2025-03-19 17:27:18 +02:00
Eirik Tsarpalis
c9ed453745 Rename project and namespaces 2025-03-19 15:02:38 +02:00
Eirik Tsarpalis
e7fa6194f6
Turn on the AOT analyzer. (#92)
* Turn on the AOT analyzer.

* Address feedback

* Add missing types.

* Fix build and react to conflicts.

* Fix failing test.

* Address feedback.
2025-03-18 18:48:28 +02:00
Stephen Halter
90dfc35fbd
Add AspNetCoreSseServer sample (#81)
* Add AspNetCoreSseMcpTransport sample

* Fix build errors after rebase

* React to McpServerFactory changes

* Make Utf8JsonWriter non thread static

* Fix test errors caused by not rebasing the HttpLister changes on factory overhaul commit
2025-03-18 12:56:44 +02:00
Peder Holdgaard Pedersen
b5a26e4c33
HttpListener based SSE Server Support (#73) 2025-03-17 14:56:03 -07:00
Stephen Toub
4597bbd7c2
Integrate Microsoft.Extensions.AI as a foundational building block (#94)
* Integrate Microsoft.Extensions.AI as a foundational building block

As with logging abstractions coming from M.E.Logging and DI abstractions coming from M.E.DependencyInjection, M.E.AI provides the relevant AI abstractions.

- Adds AsAIFunction and GetAIFunctionsAsync methods for getting functions for tools via a client.
- Uses AIFunctionFactory to implement McpServerBuilderExtensions.WithTools.
- Adds a RequestSamplingAsync overload that accepts ChatMessages/Options.
- Adds an AsSamplingChatClient method that provides an IChatClient usable on the server to request sampling of the client.
- Adds a CreateSamplingHandler method to enable an IChatClient to be easily used to satisfy sampling requests on the client.
- Deprecate McpDotNet.Extensions.AI.

* Address PR feedback
2025-03-17 16:34:00 -04:00
Stephen Toub
a0afc28ad6
Remove TextReader/Writer parameterization from StdioServerTransport (#96)
It had been added only for tests.
2025-03-17 18:30:37 +01:00
Peder Holdgaard Pedersen
34a6f1c665
Fix resource subscription handling (#89)
* Fix resource subscription handling
Add resource subscription tests

* Fix typo

* Typo

* small fixes

* Fix concurrency issue with test

* Align with specification schemas
2025-03-17 17:54:29 +01:00
Stephen Toub
08b0a04dab
Overhaul McpClientFactory/McpServerFactory (#87)
Handlers are now specified before connecting the client/server. Otherwise, race conditions exist. Handlers move into being a part of the McpClientOptions/McpServerOptions, so that they're provided to the factories. Where relevant, the handlers are specified as part of the capability descriptors, so that they go hand in hand.

The factories are no longer stateful. Instead of allocating a factory and then calling a create method on it, you just call a static factory method.
2025-03-16 12:08:52 -04:00
Peder Holdgaard Pedersen
7bf450e443
Fix docker based integration tests to be more robust (#74)
* Fix docker based integration tests to be more robust
* Make docker based integration tests manual
2025-03-15 20:56:46 +01:00
Stephen Toub
729c8b5eb9
Make logging optional in all public APIs (#72) 2025-03-13 18:16:21 -04:00
Stephen Toub
9e92bc9ce0
Remove InputOutputStreams and clean up StdioServerTransport ctors (#71)
- There doesn't appear to be a meaningful scenario for injecting different readers/writers than stdin/stdout, and having InputOutputStreams complicates the API.
- Logically folks talk about "input and output", "readers and writers", etc., and "stdio" is "standard input and output", so having input specified first is less likely to be a source of bugs.
- Logger should be optional, not required, to simplify the common and getting-started cases.
2025-03-13 12:45:30 -04:00
Stephen Toub
67320b22d5
Move functionality out of IMcpClient/Server into extensions (#60) 2025-03-13 07:55:06 -04:00
Toni Wenzel
b5076263d1
Add more tests (#51)
* Add more tests

* Fix some code smells

* Remove more code smells

* Combine "ServerIntegrationTests" and "ClientIntegrationTests" because both are client integration tests just using different servers

* Clean up resources after test

* Improve kill logic for spawned child processes

* Improved testability for Stdio

* Fix SseIntegration tests
2025-03-12 18:55:52 +01:00
Stephen Toub
fe632617dc
Update to M.E.AI 9.3.0-preview.1.25161.3 (#61) 2025-03-12 15:23:13 +01:00
Toni Wenzel
24c706b4ff
Improve code coverage (#40)
* Add tests for tool generation

* Add couple of MCPServer tests

* Fix merge issues

* More tests for McpServer

* Some more tests

* Fix merge issues
2025-03-10 18:19:49 +01:00
Toni Wenzel
709b2e0a74
Fix some code smells (#39)
* Fix some code smells

* Fix build

* Fix come issues

* Revert some changes
2025-03-10 18:03:47 +01:00
Stephen Toub
33d2b0d762
Use DescriptionAttribute and HasDefaultValue in server builder extensions (#46) 2025-03-09 11:16:00 +01:00
Peder Holdgaard Pedersen
967d7a4cc8
Fixed two reliability issues highlighted by SonarCloud (#44) 2025-03-08 20:21:14 +01:00
Eirik Tsarpalis
a2e8ecc23d
Attempt to re-enable net472 tests (#38) 2025-03-08 19:02:21 +01:00
Stephen Toub
587e05f057
Enable .NET SDK code analyzers (#41) 2025-03-08 17:18:09 +01:00
Stephen Toub
9c597aad90
Add netstandard2.0 support (#33)
* Add netstandard2.0 support
2025-03-07 20:46:40 +01:00
Toni Wenzel
ec9bdfcf5d
Improve tool handling (#28)
* Improve tool handling

* Rework stuff with new hosting options

* Update readme

* Fix build

* Try run client tests

* Fix build

* Fix issues on linux

* Try to run testserver on linux

* Fix test issue

* Fix code smells
2025-03-07 16:48:55 +01:00
Toni Wenzel
7d69499ad5
Remove warnings from build (#29)
* Remove warnings from build

* More fixes.

* Using const for transport types
2025-03-06 21:02:13 +01:00
Toni Wenzel
80daa5b73d
Add ci build workflow (#31)
* Add ci build workflow

* Using cake build

* Update readme
2025-03-06 20:49:59 +01:00
Toni Wenzel
257a97a659
Improve how to host McpServer (#30) 2025-03-05 21:15:22 +01:00