Commit graph csharp-sdk/tests
Author SHA1 Message Date
Eirik Tsarpalis
93834d1aec
Fix support for sending server notifications with request-specific McpServer instances used outside their original scope. (#832)
* Fix support for sending server notifications with request-specific McpServer instances used outside their original scope.

* Address feedback.

* Update src/ModelContextProtocol.Core/Server/SseWriter.cs
2025-10-06 18:50:33 +03:00
Stephen Halter
47b9582fd7
Make "Stateless" mode sessionless (#760)
- This removes the requirement to configure data protection which is the motivating reason for this change
- This means that McpServer.ClientInfo will always be null in stateless mode
2025-10-02 15:57:01 -07:00
Stephen Halter
740a7d0346
Move options validation to constructor (#826)
This works around the breaking change to BackgroundService in
https://github.com/dotnet/runtime/pull/116283 which launches
ExecuteAsync using Task.Run.
2025-10-02 12:38:13 -07:00
Eirik Tsarpalis
7628b3064f
Onboard repo to the .NET 10 sdk (#822)
* Onboard repo to the .NET 10 sdk

* Remove redundant property.

* Remove console logger

* Disabling failing tests in .NET 10.
2025-09-30 20:36:12 +03:00
Eirik Tsarpalis
d8768de302
Fix stdio client transport CLI argument escaping. (#811)
* Fix stdio client transport CLI argument escaping.

* Fix failing Windows test.

* Add more test cases and refine escaping logic.

* Fix test failures in mono

* Style fixes

* Tweak stdio test

* Tweak tests further
2025-09-25 14:46:39 +03:00
David Cantú
5f992994c3
Decouple Handlers and Collections from protocol types (#765)
* Decouple Handlers and Collections (e.g. ToolsHandler and ToolsCollection) from Capabilities by promoting them to the options types.

Also, group all handlers in container classes, for server, I decided to move McpServerHandlers down to Mcp.Core and move the NotificationHandlers there too.

Also, Fix ListResourceTemplatesHandler issue.

* Add remarks for empty protocol capability types

* Keep JsonIgnore properties and mark them as Obsolete

* Continue supporting Obsolete members

* Update Obsolete messages to indicate the member will be removed

* Add EditorBrowsable Never to the remaining obsolete members
2025-09-24 09:42:03 -05:00
Eirik Tsarpalis
7cfe0ef621
Add McpSessions.NegotiatedProtocolVersion property. (#794) 2025-09-19 13:11:01 +03:00
Mackinnon Buck
38b4a26998
Restructure IMcpEndpoint, IMcpClient, and IMcpServer (#723)
* Remove `McpEndpoint` & `IMcpClient`

* Revise naming, add abstract classes

* Fix missing renames

* Move extension methods to instance methods

* Add back old API, but obsoleted

* Add back obsoleted factory classes

* Move deprecation attributes

* `SseClientTransport` -> `HttpClientTransport`

* Update src/ModelContextProtocol.Core/Server/AugmentedServiceProvider.cs

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

* PR feedback

* PR feedback

* Add back dispose lock

* Fix docs projects

* PR feedback

* Undo `docs/*.md` changes

---------

Co-authored-by: Stephen Halter <halter73@gmail.com>
2025-09-16 10:22:10 -07:00
Mehran Davoudi
15d1271212
Add ElicitAsync<T> (#630) (#715)
* Add ElicitAsync<T> (#630)

- Refactored `ElicitResult` to a generic class `ElicitResult<T>` for typed content.
- Added `ElicitAsync<T>` method in `McpServerExtensions.cs` to request user input and construct schemas based on type `T`.
- Implemented schema building logic to handle primitive types and enums, ignoring unsupported types.
- Introduced `ElicitationTypedTests.cs` for testing the new elicitation functionality with typed forms.
- Verified naming policies in tests to ensure correct serialization casing.
- Defined `SampleForm` and `CamelForm` classes for expected input shapes, including unsupported properties for schema testing.
- Created JSON serialization contexts for both forms using source generation for improved performance.

* Fix the enum issue. for ElicitAsync<T>.

* Use AIJsonUtilities.CreateJsonSchema to create PrimitiveSchemaDefinition. #630

Renamed `BuildRequestSchemaFor<T>` to `BuildRequestSchema<T>`.
Updated the implementation to use `CreatePrimitiveSchema` and
enhanced type checks for supported primitives with
`AIJsonUtilities.CreateJsonSchema`. Streamlined handling of
unsupported types by consolidating return statements.

* Simplify ElicitAsync for schema validation. #630

Simplified the `ElicitAsync` method in `McpServerExtensions.cs` by removing unnecessary JSON object construction and directly deserializing `raw.Content`. Updated `CreatePrimitiveSchema` to use `JsonTypeInfo` for type checking and adjusted return logic for unsupported types.

In `ElicitationTypedTests.cs`, modified the `Can_Elicit_Typed_Information` test to account for the new `Created` property in `SampleForm`, increasing the expected property count from 5 to 6. Added assertions for the type and format of the `Created` property and included its deserialization in the test setup.

* Add error handling for unsupported elicitation types #630

Introduce exception handling in `McpServerExtensions.cs` for unsupported types. Add a test case in `ElicitationTypedTests.cs` to verify exception throwing for unsupported types. Define a new `UnsupportedForm` class with nested properties and include JSON serialization attributes for proper handling.

* Validate generic types in BuildRequestSchema #630

Introduce validation to ensure only object types are supported for elicitation requests in the `BuildRequestSchema` method. An exception is thrown for non-object types. Update the test suite with a new test case to verify this behavior, ensuring that an exception is raised when eliciting a non-object generic type (e.g., string) and that the elicitation handler is not invoked in this scenario.

* Move nullable types handling logic to ElicitationRequestParams.Coverter. #630

* Add schema validation for elicitation requests

Introduce `TryValidateElicitationPrimitiveSchema` method to validate JSON schemas for elicitation requests. This method checks for object type, verifies the "type" property, and ensures compliance with allowed properties based on primitive types (string, boolean, number). Integrate this validation in the `BuildRequestSchema` method to ensure generated schemas are valid before further processing.

* Refactor nullable type pattern matching. #630

* Update src/ModelContextProtocol.Core/Server/McpServerExtensions.cs

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

* Update src/ModelContextProtocol.Core/Server/McpServerExtensions.cs

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

* Update src/ModelContextProtocol.Core/Server/McpServerExtensions.cs

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

* Update src/ModelContextProtocol.Core/Server/McpServerExtensions.cs

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

* Update src/ModelContextProtocol.Core/Server/McpServerExtensions.cs

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

* Add ElicitResultSchemaCache. #630

* Prepopulate elicit schema validation logic. #630

- Introduced `LazyElicitAllowedProperties` to cache allowed property names for primitive types, replacing hardcoded logic.
- Streamlined `TryValidateElicitationPrimitiveSchema` method by consolidating type checks using pattern matching.
- Removed manual addition of allowed properties and now retrieve them directly from the new dictionary.
- Updated handling of "integer" type to be treated as "number" for consistency.

* Use Nullable.GetUnderlyingType to handle nullable types on elicitation. #630

- Update `ElicitRequestParams.cs` to throw a `JsonException` for non-string "type" properties, simplifying error handling.
- Modify `CreatePrimitiveSchema` in `McpServerExtensions.cs` to better handle nullable types and improve error messages.
- Revise `TryValidateElicitationPrimitiveSchema` to accept schema as the first parameter, enhancing clarity in error reporting.
- Simplify validation logic for the "type" keyword by directly retrieving string values, ensuring unsupported "type" arrays are flagged as errors.

* Rename static field.

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

* Fix static field renamings. #630

* Make BuildRequestSchema non-generic. #630

* Avoid closure allocation for serializerOptions on netcore  #630

Updated the `GetOrAdd` method to use a generic type parameter for .NET, allowing for better handling of `JsonSerializerOptions`. Retained the original implementation for other frameworks to ensure compatibility.

* Refactor ElicitRequestParams and McpServerExtensions. #630

- Simplified "type" property handling in Converter.
- Changed s_lazyElicitAllowedProperties to nullable type.
- Updated ElicitAsync to use static lambda for clarity.
- Refactored CreatePrimitiveSchema to handle nullable types.
- Initialized allowed properties in TryValidateElicitationPrimitiveSchema.
- Updated ElicitationTypedTests for naming conventions and added tests for nullable properties.
- Enforced required properties in SampleForm and introduced NullablePropertyForm.
- Added JSON source generation context for NullablePropertyForm.

* Remove reduntant checks. #630

Refactor JSON schema type handling and add integer support

This commit simplifies the validation logic for the `type` property in the JSON schema by removing unnecessary conditional checks. It directly retrieves the string value from `typeProperty` and assigns it to `typeKeyword`. Additionally, it enhances the `s_lazyElicitAllowedProperties` dictionary to include support for the `integer` type, allowing it to be recognized as a valid type with its corresponding allowed properties.

* Add IsAccepted property and update ElicitAsync return type

- Introduced `IsAccepted` property in `ElicitResult` and `ElicitResult<T>` to indicate if the action was accepted.
- Changed `ElicitAsync<T>` to return `ValueTask<ElicitResult<T>>` instead of `ValueTask<ElicitResult<T?>>`, ensuring non-nullable results.
- Updated return statements in `ElicitAsync<T>` to reflect the new return type, treating `Content` as non-nullable.

* Rename to s_elicitAllowedProperties

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

* Fix renaming s_elicitAllowedProperties. #630

* Remove unnecessary json attributes. #630

* Improve xml comment

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

* Remove extra IsAccepted property. #630

* Use IsAccepted for checks.

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

* Add IsAccepted to non-generic ElicitResult.

---------

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
Co-authored-by: Stephen Toub <stoub@microsoft.com>
2025-09-15 17:50:50 +03:00
Stephen Halter
d344c65120
Add middleware and authz support for server-side handlers (#733) 2025-09-10 10:07:45 -07:00
S-Luiten
f9129f5b28
Improvements to dynamic client registration (#609)
* Add a delegate for handling the dynamic client registration response.
* Support passing an initial access token for dynamic client registration.
2025-08-26 14:24:00 -07:00
Stephen Toub
2fa658e439
Add WithXx overloads that take target instance (#706)
* Add WithXx overloads that take target instance
* Special-case enumerables
2025-08-26 09:38:53 -07:00
Eirik Tsarpalis
f2dc714cd5
Update packages and Infra (#732)
* Update packages and Infra

* Update Directory.Packages.props

* Update Directory.Packages.props

* Install mono on macos gh-actions machines

* Skip failing test

* Attempt to fix failing macos test.
2025-08-26 17:28:35 +03:00
Stephen Halter
b067261ca7
Prune idle sessions before starting new ones (#701) 2025-08-18 11:04:27 -07:00
Stephen Toub
70960e1f1b
Remove special-casing of string enumerables in McpServerTool (#699)
We special-case string enumerables, translating them to an array of text content blocks, but other enumerables just get serialized, and there's a reasonable expectation that returning a string[] would produce a JSON array of strings. Just delete the special-casing.
2025-08-13 13:05:42 -04:00
Copilot
e1a2564a4e
Fix NotSupportedException when returning IEnumerable<ContentBlock> (#675) 2025-08-05 14:36:11 -07:00
Stephen Halter
5e5b1af29b
Remove 'Sse' from AspNetCoreSseServer sample name (#665) 2025-08-01 10:47:11 -07:00
Copilot
650df63194
Fix ResourceLinkBlock deserialization by adding missing "name" case (#645)
* Initial plan

* Initial analysis and plan for ResourceLinkBlock deserialization fix

Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>

* Fix ResourceLinkBlock deserialization by adding missing "name" case

Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>

* Remove unnecessary dotnet-install.sh file

Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>

* Use const for JSON string variables and improve indentation

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

* Remove temporary backup file

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

* Change JSON variable names to use PascalCase (Json) as requested

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
2025-07-23 19:52:45 +03:00
Stephen Halter
f53e0d2132
Respect HandleResponse() and SkipHandler() calls in OnResourceMetadataRequest (#607) 2025-07-17 17:39:05 -07:00
Eirik Tsarpalis
3156818d66
Update MEAI version and add regression test for #601. (#628) 2025-07-16 16:17:49 +03:00
Stephen Halter
a88ef0deb7
Flow ExecutionContext with JsonRpcMessage (#616)
The primary goal of this change is to support IHttpContextAccessor in tool calls when the Streamable HTTP is in its default non-Stateless mode.
2025-07-15 13:58:43 -07:00
David Parks
984aa9ae1b
fix: Prevent crash when Options.ResourceMetadata is null but handled by event (#603) 2025-07-09 12:05:00 -07:00
Eirik Tsarpalis
aa75f6f37e
Enable netfx testing. (#588)
* Enable netfx testing.

* Skip impacted tests and re-enable netfx testing.

* Remove redundant comment.

* Remove unnecessary project configuration.

* Simplify test server copying.
2025-07-08 18:50:37 +03:00
Den Delimarsky 🌺
cdf88e7f5d
Authorization Support (Using ASP.NET Core Native AuthN/AuthZ Integration) (#377) 2025-07-03 14:39:28 -07:00
Stephen Toub
4411f1ce5e
Change default name casing of McpServerXx.Create tools/prompts/resources (#568) 2025-07-01 16:40:42 -04:00
Stephen Toub
4e290f8de9
Add CallToolResult.ToChatMessage extension method (#575)
To simplify non-AIFunction usage getting a ChatMessage with the appropriate FunctionResultContent.
2025-07-01 07:09:49 -04:00
Stephen Toub
ab51400ba0
Make CallToolResult.IsError optional (#573)
Match the spec, and avoid sending the property in the success case.
2025-06-30 11:35:07 -04:00
Stephen Toub
f3539cc2ca
Enable injecting IMcpServer and friends into ctors (#570) 2025-06-30 10:58:08 -04:00
Eirik Tsarpalis
55bda787ae
Styling: remove a few redundant parens from object initializer expressions. (#548)
* Styling: remove a few redundant parens from object initializer expressions.

* Fix build error.
2025-06-26 16:55:54 +03:00
Stephen Toub
068f0418fe
Fix deserialization of PrimitiveSchemaDefinition (#537)
This is preventing clients from correctly handling elicitation requests.
2025-06-24 08:48:12 -04:00
Eirik Tsarpalis
0b26b7bf22
Fix URI template derivation and lookup (#530)
* Use lowercase host in resource URI template derivation.

* Address feedback

* Add casing testing for non-templated resource lookups.

* Fix uri primitive lookup

* Use a hardcoded mcp hostname in derived resource template URIs and ensure built-in resources work with the VS Code client.

* Address feedback

* Fix equality comparison

* Use dedicated type for resource collections.
2025-06-23 19:49:38 +03:00
Stephen Toub
0e6184a4aa
Add 2025-06-18 as supported version (#529) 2025-06-20 12:03:28 -04:00
Stephen Halter
a4b4edaa2a
Send DELETE request when closing a Streamable HTTP session on the client (#501) 2025-06-19 08:40:11 -07:00
Stephen Halter
befa31d5c9
Send MCP-Protocol-Version header in Streamable HTTP client transport (#500) 2025-06-17 14:27:26 -07:00
Stephen Toub
b9b4a23005
Add a callback for receiving stderr output (#525)
* Add a callback for receiving stderr output

* Add asserts to test
2025-06-17 16:03:20 -04:00
Stephen Toub
2bcff315f8
Update content according to latest spec (#513)
* Update content according to latest spec

* Add Title to a bunch of types

* Add _meta to lots of types

* Address feedback, more cleanup, fix NAOT test failure
2025-06-16 19:39:41 -04:00
Sven Boemer
28d65470f4
Update Microsoft.Extensions.AI version & expose JSON configuration on McpServerResourceCreateOptions (#511)
* Update Microsoft.Extensions.AI version

* Update Microsoft.Extensions.AI.OpenAI

* Fix failing tests; add JSON configuration options to McpServerResourceCreateOptions.

* Replace temporary code with `AIFunction.ReturnJsonSchema`

---------

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
2025-06-13 13:50:14 +01:00
Eirik Tsarpalis
1e8fb04037
Make enums serialize as strings if using the reflection-based serializer. (#473)
* Make enums serialize as strings if using the reflection-based serializer.

* Remove unnecessary partial

* Use an explicit reflection-based converter for enums.

* Update comments

* Make the reflection-based enum converter public.
2025-06-12 18:25:54 +03:00
Eirik Tsarpalis
d7d9eab3f8
Pass session id's to MCP endpoints. (#466)
* Pass session id's to MCP endpoints.

* Update src/ModelContextProtocol/Client/McpClient.cs

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

* Update src/ModelContextProtocol.AspNetCore/StreamableHttpHandler.cs

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

* Make init callback asynchronous.

* Address feedback.

---------

Co-authored-by: Stephen Halter <halter73@gmail.com>
2025-06-12 17:56:57 +03:00
Eirik Tsarpalis
fa017c04c3
Add structured output/output schema support for server-side tools. (#480)
* Add structured output/output schema support for server-side tools.

* Address feedback.

* Add McpServerToolAttribute.UseStructuredContent

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
2025-06-06 00:20:12 -07:00
Stephen Halter
046903c27b
Don't conflate request and response IDs in Streamable HTTP transports (#475) 2025-06-03 12:17:11 -07:00
Stephen Toub
e0b058f260
Improve protocol version handling (#468)
Right now by default, the client and server hardcode the initial spec version, and if there's a mismatch, the request fails.

With this change, by default both client and server are allowed to float to any supported version.
2025-06-02 23:03:57 -04:00
Stephen Halter
703b0fe50b
Add ModelContextProtocol.Core package (#428) 2025-06-02 15:16:45 -07:00
Stephen Toub
5cc7c00456
Add elicitation capability (#467)
* Add elicitation capability
2025-05-29 22:03:03 -04:00
Stephen Halter
317f468807
Automatically fall back from Streamable HTTP to SSE on the client by default (#456) 2025-05-28 11:06:58 -07:00
кɵɵѕнī
adb2098e4d
Log tool invocation errors (#447)
* Log tool errors
* add test and implement source generated logging
2025-05-23 14:48:47 -07:00
Eirik Tsarpalis
77f9e64e04
SSE client transport: tolerate return of RPC responses in POST requests. (#445)
* SSE client transport: handle return of RPC responses in POST requests.

* Remove accidental change.

* Address feedback

* Remove unused namespace.
2025-05-23 21:39:54 +03:00
Stephen Halter
d23f442a69
Allow more accept headers (#429) 2025-05-19 17:31:20 -07:00
Stephen Toub
106ab8064f
Clean up namespaces (#410)
No meaningful code changes, just lots of namespace shuffling to have fewer namespaces and to put things in better namespaces.
2025-05-14 14:50:26 -04:00
Stephen Toub
c11c634945
Improve exception diagnostics for stdio client (#376)
* Improve exception diagnostics for stdio client

Track the last several lines of stderr and include those in an exception created as part of CleanupAsync if the server process has already exited. This is based on an assumption that the server should never exit prior to CleanupAsync being called.

* Address feedback and fix test failure
2025-05-14 10:46:49 -04:00