alpha

Guides building Model Context Protocol servers with the C# SDK: packages, tool and prompt attributes, stdio transport, dependency injection and sampling.
description: 'Instructions for building Model Context Protocol (MCP) servers using the C# SDK'
applyTo: '**/.cs, **/.csproj'
dotnet add package ModelContextProtocol --prereleaseLogToStandardErrorThreshold = LogLevel.Trace to avoid interfering with stdio transport[McpServerToolType] attribute on classes containing MCP tools[McpServerTool] attribute on methods to expose them as tools[Description] attribute from System.ComponentModel to document tools and parametersMcpServer, HttpClient, or other services as parametersMcpServer.AsSamplingChatClient() to make sampling requests back to the client from within tools[McpServerPromptType] on classes and [McpServerPrompt] on methodsWithStdioServerTransport() when building the serverWithToolsFromAssembly() to auto-discover and register all tools from the current assemblyTask or Task<T>)CancellationToken parameters in async tools for proper cancellation supportMcpServerOptions with custom handlers like ListToolsHandler and CallToolHandlerMcpProtocolException for protocol-level errors with appropriate McpErrorCode valuesMcpClient from the same SDK or any compliant MCP clientMcpProtocolException with McpErrorCode.InvalidParams for invalid inputs[McpServerToolType]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 MyTools
{
[McpServerTool, Description("Description of what the tool does")]
public static string ToolName(
[Description("Parameter description")] string param) =>
$"Result: {param}";
}
[McpServerTool, Description("Fetches data from a URL")]
public static async Task<string> FetchData(
HttpClient httpClient,
[Description("The URL to fetch")] string url,
CancellationToken cancellationToken) =>
await httpClient.GetStringAsync(url, cancellationToken);
[McpServerTool, Description("Analyzes content using the client's LLM")]
public static async Task<string> Analyze(
McpServer server,
[Description("Content to analyze")] string content,
CancellationToken cancellationToken)
{
var messages = new ChatMessage[]
{
new(ChatRole.User, $"Analyze this: {content}")
};
return await server.AsSamplingChatClient()
.GetResponseAsync(messages, cancellationToken: cancellationToken);
}
© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com


Sea turtle carrying floating film studio island
Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.
image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost
Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.
video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

Cozy Pixel Art Farm Island Village
Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.
image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape
Loading reviews…
Loading comments…