Skip to content

Commit 9b04216

Browse files
committed
Configure hosted stdio servers to log to stderr
1 parent b9bb871 commit 9b04216

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,24 @@ It includes a simple echo tool as an example (this is included in the same file
8383
the employed overload of `WithTools` examines the current assembly for classes with the `McpServerToolType` attribute, and registers all methods with the
8484
`McpTool` attribute as tools.)
8585

86+
```
87+
dotnet add package ModelContextProtocol --prerelease
88+
dotnet add package Microsoft.Extensions.Hosting
89+
```
90+
8691
```csharp
8792
using Microsoft.Extensions.DependencyInjection;
8893
using Microsoft.Extensions.Hosting;
94+
using Microsoft.Extensions.Logging;
8995
using ModelContextProtocol.Server;
9096
using System.ComponentModel;
9197

9298
var builder = Host.CreateEmptyApplicationBuilder(settings: null);
99+
builder.Logging.AddConsole(consoleLogOptions =>
100+
{
101+
// Configure all logs to go to stderr
102+
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
103+
});
93104
builder.Services
94105
.AddMcpServer()
95106
.WithStdioServerTransport()
@@ -101,8 +112,7 @@ public static class EchoTool
101112
{
102113
[McpServerTool, Description("Echoes the message back to the client.")]
103114
public static string Echo(string message) => $"hello {message}";
104-
}
105-
```
115+
}```
106116

107117
Tools can have the `IMcpServer` representing the server injected via a parameter to the method, and can use that for interaction with
108118
the connected client. Similarly, arguments may be injected via dependency injection. For example, this tool will use the supplied

tests/ModelContextProtocol.TestServer/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ private static ILoggerFactory CreateLoggerFactory()
2424

2525
return LoggerFactory.Create(builder =>
2626
{
27+
builder.AddConsole(options =>
28+
{
29+
options.LogToStandardErrorThreshold = LogLevel.Trace;
30+
});
2731
builder.AddSerilog();
2832
});
2933
}

0 commit comments

Comments
 (0)