|
| 1 | +# ASP.NET Core MCP server sample |
| 2 | + |
| 3 | +This sample hosts an MCP server with Streamable HTTP and configures OpenTelemetry for MCP operations, ASP.NET Core requests, outgoing HTTP requests, metrics, and structured logs. |
| 4 | + |
| 5 | +## Export to Application Insights |
| 6 | + |
| 7 | +Set the Application Insights connection string and run the sample: |
| 8 | + |
| 9 | +```bash |
| 10 | +export APPLICATIONINSIGHTS_CONNECTION_STRING='InstrumentationKey=<your-instrumentation-key>;IngestionEndpoint=https://<region>.in.applicationinsights.azure.com/' |
| 11 | +dotnet run --project samples/AspNetCoreMcpServer |
| 12 | +``` |
| 13 | + |
| 14 | +PowerShell: |
| 15 | + |
| 16 | +```powershell |
| 17 | +$env:APPLICATIONINSIGHTS_CONNECTION_STRING = 'InstrumentationKey=<your-instrumentation-key>;IngestionEndpoint=https://<region>.in.applicationinsights.azure.com/' |
| 18 | +dotnet run --project samples/AspNetCoreMcpServer |
| 19 | +``` |
| 20 | + |
| 21 | +The connection string is read through ASP.NET Core configuration. Keep it outside source control and use your deployment platform's secret configuration in production. |
| 22 | + |
| 23 | +When `APPLICATIONINSIGHTS_CONNECTION_STRING` is absent or empty, the sample retains its default OTLP exporter. Configure that path with standard `OTEL_EXPORTER_OTLP_*` environment variables. |
| 24 | + |
| 25 | +## Signals and MCP attributes |
| 26 | + |
| 27 | +Application Insights receives the signals already emitted by the SDK and the configured ASP.NET Core/OpenTelemetry instrumentation: |
| 28 | + |
| 29 | +- server activities appear as requests and client activities appear as dependencies; |
| 30 | +- structured logs appear as traces; |
| 31 | +- MCP histograms appear as custom metrics; |
| 32 | +- HTTP server and client instrumentation provides the surrounding transport spans. |
| 33 | + |
| 34 | +MCP activities can include attributes such as `mcp.method.name`, `mcp.protocol.version`, `mcp.session.id`, `jsonrpc.request.id`, and `gen_ai.tool.name`. The exact attributes depend on the operation and whether sensitive-data telemetry is enabled. |
| 35 | + |
| 36 | +For example, query MCP request and dependency telemetry in Logs: |
| 37 | + |
| 38 | +```kusto |
| 39 | +union withsource=TelemetryType requests, dependencies |
| 40 | +| extend McpMethod = tostring(customDimensions["mcp.method.name"]), |
| 41 | + ToolName = tostring(customDimensions["gen_ai.tool.name"]), |
| 42 | + McpSession = tostring(customDimensions["mcp.session.id"]) |
| 43 | +| where isnotempty(McpMethod) |
| 44 | +| project timestamp, TelemetryType, name, success, resultCode, McpMethod, ToolName, McpSession |
| 45 | +| order by timestamp desc |
| 46 | +``` |
| 47 | + |
| 48 | +Invalid tool calls, malformed JSON-RPC messages, and other failures appear only when the current SDK or ASP.NET Core instrumentation emits a log or activity for that path. This sample exports existing telemetry; it does not add custom protocol events or per-tool instrumentation. |
0 commit comments