Commit graph

219 commits

Author SHA1 Message Date
Jeff Handley
4dd2f42b82
Add SECURITY.md (#239) 2025-04-08 11:43:02 -07:00
Stephen Halter
bab74d5895
Fix StdioServerTransport.DisposeAsync hang (#235)
- It's not just Unix, it's all platforms that can hang
- Windows doesn't hang on Ctrl-C because that triggers
  the end of the Stream, but that doesn't help in IHostApplicationLifetime.StopAsync() scenarios

4e9627e311/src/libraries/System.Console/src/System/ConsolePal.Unix.ConsoleStream.cs (L13)
4e9627e311/src/libraries/System.Console/src/System/ConsolePal.Windows.cs (L1149)
2025-04-08 10:20:16 -07:00
Eirik Tsarpalis
3e21f3594a
Remove McpServerConfig and have McpClientFactory accept IClientTransport instances directly. (#230)
* Remove McpServerConfig and have McpClientFactory accept IClientTransport instances directly.

* Address feedback and use lists when representing stdio arguments.

* Use "Name" for transport descriptors and "EndpointName" for MCP clients and servers.

* Update code samples.

* Fix source file naming
2025-04-08 17:37:41 +01:00
Esteban Solano Granados
8bd9ee81c0
Fix api.weather.gov/points response handling in WeatherTools (#134) 2025-04-07 22:39:10 -07:00
Stephen Toub
3b83ad4103
Enable servers to log to the clients via ILogger (#229)
* Enable servers to log to the client via ILogger

* Address feedback
2025-04-07 23:16:42 -04:00
Stephen Halter
3a2e9a228d
Fix race in KestrelInMemoryConnection (#233)
* Also remove some duplication from WithStdioServerTransport and WithStreamServerTransport
2025-04-07 14:49:32 -07:00
Stephen Toub
82510d5629
Add server "completions" capability (#232)
* Split apart Capabilities.cs

No code changes, just moving each type to its own file.

* Add server completions capability
2025-04-07 16:59:37 -04:00
Eirik Tsarpalis
ac0bc3a1f1
Bump xunit.v3 to 2.0.0 (#231) 2025-04-07 14:46:24 -04:00
Stephen Toub
5d7c2a1a0f
Stop the host when the single session server service finishes (#226)
* Stop the host when the single session server service finishes

* Add test
2025-04-07 12:45:17 -04:00
Stephen Toub
03dc90aa33
Use strong-typing of params in most remaining McpClientExtensions methods (#224) 2025-04-07 11:30:42 -04:00
Stephen Halter
faf12b6a94
Use Kestrel for all in-memory HTTP tests (#225)
* Add route pattern parameter to MapMcp
* Add configureOptionsAsync
* Use Kestrel for all in-memory HTTP tests
* Use ApplicationStopping token for SSE responses
* Use RegisterNotificationHandler in MapMcp tests
* Work around SocketsHttpHandler bug where it doesn't call DispseAsync on the stream returned by the ConnectCallback
2025-04-06 19:27:17 -07:00
Stephen Toub
848c9df433
Allow notification handlers post-creation temporary registration (#223)
- We still allow notification handlers to be configured pre-creation, eliminating any race conditions that may result from missed notifications. But now we also allow for post-creation notification handler registration.
- Handlers may now also be removed. This permits temporary registrations that may be created in a scoped manner.
- Registration handling is thread-safe.
- Handlers are now cancelable.
- Exceptions from notification handlers trigger all normal exception handling in the dispatch pipeline.

This also:
- Adds a WithStreamServerTransport as a counterpart to WithStdioServerTransport. The latter was used in several tests in a hacky way, and that's now simplified via the former's existence.
2025-04-06 21:17:00 -04:00
Stephen Toub
f135355bf9
Propagate fix from AIFunctionFactory to TemporaryAIFunctionFactory (#215) 2025-04-04 21:55:33 -04:00
Eirik Tsarpalis
5e606c1543
Bump package version to 0.1.0-preview.7 (#212) 2025-04-04 19:53:08 +01:00
Aaron Powell
01410df5ad
Add EverythingServer sample (#151) 2025-04-04 11:48:53 -07:00
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