csharp-sdk/samples/AspNetCoreMcpServer
Repository files (latest commit first)
Filename Latest commit message Latest commit date
pardhu c6a97d94d6
Add HttpServerSessionMode for hybrid stateful/stateless HTTP serving (#1796)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jeff Handley <jeffhandley@users.noreply.github.com>
2026-08-12 23:36:36 -07:00
..
Properties Add middleware and authz support for server-side handlers (#733) 2025-09-10 10:07:45 -07:00
Resources Remove 'Sse' from AspNetCoreSseServer sample name (#665) 2025-08-01 10:47:11 -07:00
Tools Add McpMetaAttribute for attaching metadata to tools, prompts, and resources (#857) 2025-10-17 09:02:54 -04:00
appsettings.Development.json Remove 'Sse' from AspNetCoreSseServer sample name (#665) 2025-08-01 10:47:11 -07:00
appsettings.json Document and demonstrate allowed hosts and CORS policy guidance (#1554) 2026-05-01 19:03:30 -07:00
AspNetCoreMcpServer.csproj Add Application Insights telemetry example (#1778) 2026-08-04 16:25:19 -07:00
Program.cs Add HttpServerSessionMode for hybrid stateful/stateless HTTP serving (#1796) 2026-08-12 23:36:36 -07:00
README.md Add Application Insights telemetry example (#1778) 2026-08-04 16:25:19 -07:00

ASP.NET Core MCP server sample

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.

Export to Application Insights

Set the Application Insights connection string and run the sample:

export APPLICATIONINSIGHTS_CONNECTION_STRING='InstrumentationKey=<your-instrumentation-key>;IngestionEndpoint=https://<region>.in.applicationinsights.azure.com/'
dotnet run --project samples/AspNetCoreMcpServer

PowerShell:

$env:APPLICATIONINSIGHTS_CONNECTION_STRING = 'InstrumentationKey=<your-instrumentation-key>;IngestionEndpoint=https://<region>.in.applicationinsights.azure.com/'
dotnet run --project samples/AspNetCoreMcpServer

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.

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.

Signals and MCP attributes

Application Insights receives the signals already emitted by the SDK and the configured ASP.NET Core/OpenTelemetry instrumentation:

  • server activities appear as requests and client activities appear as dependencies;
  • structured logs appear as traces;
  • MCP histograms appear as custom metrics;
  • HTTP server and client instrumentation provides the surrounding transport spans.

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.

For example, query MCP request and dependency telemetry in Logs:

union withsource=TelemetryType requests, dependencies
| extend McpMethod = tostring(customDimensions["mcp.method.name"]),
         ToolName = tostring(customDimensions["gen_ai.tool.name"]),
         McpSession = tostring(customDimensions["mcp.session.id"])
| where isnotempty(McpMethod)
| project timestamp, TelemetryType, name, success, resultCode, McpMethod, ToolName, McpSession
| order by timestamp desc

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.