Mcp

From managedcode/dotnet-skills · View interactive version

Mcp is an AI agent skill from the GitHub repository managedcode/dotnet-skills. Build or consume Model Context Protocol (MCP) servers and clients in .NET using the official MCP C# SDK, including stdio, Streamable HTTP, tools, prompts, resources, and capability negotiation. USE FOR: .NET MCP servers or clients; stdio versus HTTP transport choices; tools, resources, prompts, comp

How to use this skill

Skill file

MCP C# SDK for .NET

Trigger On

Use This Skill Instead Of

Documentation

References

Load only what the task needs:

Package Selection

| Package | Choose when |

|---------|-------------|

| ModelContextProtocol.Core | You only need a client or low-level server APIs and want the smallest dependency set. |

| ModelContextProtocol | You want the main SDK package with hosting, DI, attribute discovery, and stdio server support. Start here for most projects. |

| ModelContextProtocol.AspNetCore | You are hosting a remote MCP server in ASP.NET Core over HTTP. This includes the main package. |

Transport Selection

| Transport | Use when | Notes |

|-----------|----------|-------|

| StdioClientTransport / WithStdioServerTransport() | The MCP server should run as a local child process. | Best for local tooling and editor/agent integrations. |

| HttpClientTransport + HttpTransportMode.StreamableHttp | The server is remote or should be reachable over HTTP. | Recommended HTTP transport; supports streaming and session resumption. |

| HttpTransportMode.Sse | You must connect to an older SSE-only server. | Legacy compatibility only; do not choose this for new servers. |

Current v2.2 Notes

flowchart LR
    A["Need MCP interoperability in .NET"] --> B{"Role?"}
    B -->|"Expose MCP surface"| C{"Where will it run?"}
    B -->|"Consume an MCP server"| D{"Transport?"}
    C -->|"Local child process"| E["ModelContextProtocol\nAddMcpServer()\nWithStdioServerTransport()"]
    C -->|"Remote HTTP endpoint"| F["ModelContextProtocol.AspNetCore\nAddMcpServer()\nWithHttpTransport()\nMapMcp()"]
    D -->|"stdio"| G["StdioClientTransport\nMcpClient.CreateAsync()"]
    D -->|"HTTP"| H["HttpClientTransport\nAutoDetect or StreamableHttp"]
    E --> I["Register tools/resources/prompts"]
    F --> I
    G --> J["Check ServerCapabilities\nbefore optional features"]
    H --> J

Workflow

  1. Pick the package and transport first.
  1. Model the MCP surface explicitly.
  1. Prefer attribute discovery for straightforward servers.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using System.ComponentModel;

var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(options =>
{
    options.LogToStandardErrorThreshold = LogLevel.Trace;
});

builder.Services
    .AddMcpServer()
    .WithStdioServerTransport()
    .WithToolsFromAssembly();

await builder.Build().RunAsync();

[McpServerToolType]
public static class EchoTool
{
    [McpServerTool, Description("Echoes the message back to the client.")]
    public static string Echo(string message) => $"hello {message}";
}
  1. For HTTP servers, use the ASP.NET Core transport and map the endpoint directly.
using ModelContextProtocol.Server;
using System.ComponentModel;

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddMcpServer()
    .WithHttpTransport()
    .WithToolsFromAssembly();

var app = builder.Build();
app.MapMcp("/mcp");
app.Run();

[McpServerToolType]
public static class EchoTool
{
    [McpServerTool, Description("Echoes the message back to the client.")]
    public static string Echo(string message) => $"hello {message}";
}
  1. When consuming a server, use McpClient.CreateAsync(...) and stay capability-aware.
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol;

var transport = new StdioClientTransport(new StdioClientTransportOptions
{
    Name = "Everything",
    Command = "npx",
    Arguments = ["-y", "@modelcontextprotocol/server-everything"],
});

await using var client = await McpClient.CreateAsync(transport);

IList<McpClientTool> tools = await client.ListToolsAsync();

if (client.ServerCapabilities.Prompts is not null)
{
    var prompts = await client.ListPromptsAsync();
}
  1. Treat optional features as negotiated capabilities, not assumptions.
  1. Keep HTTP guidance current.
  1. Treat the .NET AI MCP quickstarts as bootstrap examples.
  1. Respect current error and serialization rules.

Anti-Patterns To Avoid

| Anti-pattern | Why it causes trouble | Better approach |

|--------------|-----------------------|-----------------|

| Picking HTTP transport for a purely local child-process scenario | Adds unnecessary hosting, auth, and deployment surface | Use stdio for local/editor-hosted integrations |

| Treating SSE as the default remote transport | Locks new work to legacy behavior | Prefer Streamable HTTP and keep SSE only for backward compatibility |

| Writing tools without [Description] metadata | Hosts and models lose schema clarity | Describe tool purpose and parameters explicitly |

| Returning huge binary/text payloads from every tool call | Bloats context and slows hosts | Return focused content and move large data to resources |

| Logging to stdout on stdio servers | Corrupts the protocol stream | Send logs to stderr |

| Assuming prompts/resources/logging/completions exist | Breaks against partial implementations | Check negotiated capabilities first |

| Using filters for normal business logic | Makes handlers opaque and hard to reason about | Keep filters for cross-cutting policy, audit, or protocol plumbing |

Deliver

Validate

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.