Skip to content

Add articles on logging and progress #717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

mikekistler
Copy link
Contributor

This PR adds a couple more conceptual docs articles, and introduces some structure into the set of conceptual docs.


[!code-csharp[](samples/client/Program.cs?name=snippet_LoggingCapabilities)]

If the server supports logging, the client can set the level of log messages it wishes to receive with
Copy link
Collaborator

@PederHP PederHP Aug 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec has been, and still is I think, a little unclear on whether this is a requirement or not. There may be servers using other SDKs, which don't start sending logging message until a log level has been set. So I would say it's good practice to always call if the server supports logging and the client wants to receive messages.


[Initialization]: https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle#initialization

Servers built with the C# SDK always declare the logging capability. The C# SDK provides an extension method
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about stateless http servers? It's difficult to support logging in a meaningful way with these.


[progress tracking]: https://modelcontextprotocol.io/specification/2025-06-18/basic/utilities/progress

Typically progress tracking is supported by server tools that perform operations that take a significant amount of time to complete, such as data processing or complex calculations.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image generation is also a classic.

[IMcpServer]: https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.Server.IMcpServer.html
[ProgressNotificationParams]: https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.Protocol.ProgressNotificationParams.html

The server should verify that the caller provided a `progressToken` in the request and include it in the call to [sendNotificationAsync]. The following example demonstrates how a server can send a progress notification:
Copy link
Collaborator

@PederHP PederHP Aug 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think this is a must and not a should. But I guess this is an article / sample and not a spec, so maybe should is better English. Not my first language, so I could be wrong on this. Mentioning it just in case.

}).ConfigureAwait(false);
```

The second way is to pass a [Progress<T>] instance to the tool method. The MCP C# SDK will automatically handle progress notifications and report them through the [Progress<T>] instance. This notification handler will only receive progress updates for the specific request that was made, rather than all progress notifications from the server.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Developers might want guidance on what T they can / should use in this section.


The server should verify that the caller provided a `progressToken` in the request and include it in the call to [sendNotificationAsync]. The following example demonstrates how a server can send a progress notification:

[!code-csharp[](samples/server/Tools/LongRunningTools.cs?name=snippet_SendProgress)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This snippet appears to not load in the rendered docs:
image

description:
uid: progress
---
The Model Context Protocol (MCP) supports [progress tracking] for long-running operations through notification messages.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: consider inserting a header reflecting the title here:

image

[RegisterNotificationHandler]: https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.IMcpEndpoint.html#ModelContextProtocol_IMcpEndpoint_RegisterNotificationHandler_System_String_System_Func_ModelContextProtocol_Protocol_JsonRpcNotification_System_Threading_CancellationToken_System_Threading_Tasks_ValueTask__

```csharp
mcpClient.RegisterNotificationHandler(NotificationMethods.ProgressNotification,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: consider using a compiled snippet so that the docs don't go out of sync in the future.

}).ConfigureAwait(false);
```

The second way is to pass a [Progress<T>] instance to the tool method. The MCP C# SDK will automatically handle progress notifications and report them through the [Progress<T>] instance. This notification handler will only receive progress updates for the specific request that was made, rather than all progress notifications from the server.
Copy link
Member

@eiriktsarpalis eiriktsarpalis Aug 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Angle brackets won't render in raw markdown:

Suggested change
The second way is to pass a [Progress<T>] instance to the tool method. The MCP C# SDK will automatically handle progress notifications and report them through the [Progress<T>] instance. This notification handler will only receive progress updates for the specific request that was made, rather than all progress notifications from the server.
The second way is to pass a [`Progress<T>`] instance to the tool method. The MCP C# SDK will automatically handle progress notifications and report them through the [`Progress<T>`] instance. This notification handler will only receive progress updates for the specific request that was made, rather than all progress notifications from the server.


### Server Implementation

When processing a request, the server can use the [sendNotificationAsync] extension method of [IMcpServer] to send progress updates,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When processing a request, the server can use the [sendNotificationAsync] extension method of [IMcpServer] to send progress updates,
When processing a request, the server can use the [`sendNotificationAsync`] extension method of [`IMcpServer`] to send progress updates,

specifying `"notifications/progress"` as the notification method name.
The C# SDK registers an instance of [IMcpServer] with the dependency injection container,
so tools can simply add a parameter of type [IMcpServer] to their method signature to access it.
The parameters passed to [sendNotificationAsync] should be an instance of [ProgressNotificationParams], which includes the current progress, total steps, and an optional message.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The parameters passed to [sendNotificationAsync] should be an instance of [ProgressNotificationParams], which includes the current progress, total steps, and an optional message.
The parameters passed to [`sendNotificationAsync`] should be an instance of [`ProgressNotificationParams`], which includes the current progress, total steps, and an optional message.


The second way is to pass a [Progress<T>] instance to the tool method. The MCP C# SDK will automatically handle progress notifications and report them through the [Progress<T>] instance. This notification handler will only receive progress updates for the specific request that was made, rather than all progress notifications from the server.

[Progress<T>]: https://learn.microsoft.com/en-us/dotnet/api/system.progress-1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[Progress<T>]: https://learn.microsoft.com/en-us/dotnet/api/system.progress-1
[`Progress<T>`]: https://learn.microsoft.com/en-us/dotnet/api/system.progress-1

MCP Servers using the MCP C# SDK can obtain an [ILoggerProvider] from the IMcpServer [AsClientLoggerProvider] extension method,
and from that can create an [ILogger] instance for logging messages that should be sent to the MCP client.

[!code-csharp[](samples/server/Tools/LoggingTools.cs?name=snippet_LoggingConfiguration)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This snippet is not loading in the render:

image


[NotificationMethods.LoggingMessageNotification]: https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.Protocol.NotificationMethods.html#ModelContextProtocol_Protocol_NotificationMethods_LoggingMessageNotification

[!code-csharp[](samples/client/Program.cs?name=snippet_LoggingHandler)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise, this snippet is not loading in the render.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants