Documentation Index
Fetch the complete documentation index at: https://mintlify.com/modelcontextprotocol/csharp-sdk/llms.txt
Use this file to discover all available pages before exploring further.
Overview
TheIMcpServerBuilder interface provides a fluent API for configuring Model Context Protocol (MCP) servers when using dependency injection. It exposes methods for registering tools, prompts, resources, custom request handlers, and server transports through a chain of method calls.
Interface Definition
Gets the associated service collection for registering additional services and dependencies.
Obtaining the Builder
The builder is obtained from theAddMcpServer extension method:
Builder Extension Methods
Tool Registration
Discovers and registers all methods on
TToolType marked with [McpServerTool] attribute.Parameters:serializerOptions(optional): JSON serializer options for parameter marshalling
- For instance methods, a new instance is created for each tool invocation
- For static methods, no instance is created
- Supports both public and non-public methods
Registers tools from a specific instance. Methods are invoked on the provided target.Special Case: If
TToolType is IEnumerable<McpServerTool>, tools are registered directly without method scanning.Registers pre-created
McpServerTool instances directly.Registers tools from multiple types. Each type’s
[McpServerTool] attributed methods are discovered.Requires unreferenced code. Not compatible with Native AOT. Use generic
WithTools<T> instead for AOT scenarios.Scans an assembly for types marked with
[McpServerToolType] and registers all their [McpServerTool] methods.Parameters:toolAssembly: Assembly to scan. If null, uses calling assembly.serializerOptions(optional): JSON serializer options
Requires unreferenced code. Not compatible with Native AOT.
Prompt Registration
Discovers and registers all methods on
TPromptType marked with [McpServerPrompt] attribute.Registers prompts from a specific instance.Special Case: If
TPromptType is IEnumerable<McpServerPrompt>, prompts are registered directly.Registers pre-created
McpServerPrompt instances.Registers prompts from multiple types.
Requires unreferenced code. Not compatible with Native AOT.
Scans assembly for
[McpServerPromptType] classes and registers their [McpServerPrompt] methods.Requires unreferenced code. Not compatible with Native AOT.
Resource Registration
Discovers and registers all methods on
TResourceType marked with [McpServerResource] attribute.Registers resources from a specific instance.Special Case: If
TResourceType is IEnumerable<McpServerResource>, resources are registered directly.Registers pre-created
McpServerResource instances.Registers resources from multiple types.
Requires unreferenced code. Not compatible with Native AOT.
Scans assembly for
[McpServerResourceType] classes and registers their [McpServerResource] methods.Requires unreferenced code. Not compatible with Native AOT.
Request Handlers
Configures a handler for listing available tools.
Typically paired with
WithCallToolHandler for complete tool implementation.Configures a handler for executing tool calls.
Configures a handler for listing available prompts.
Typically paired with
WithGetPromptHandler.Configures a handler for retrieving prompt content.
Configures a handler for listing available resources.
Typically paired with
WithReadResourceHandler.Configures a handler for reading resource content.
Configures a handler for listing resource templates (URI patterns).
Configures a handler for auto-completion suggestions for prompt arguments or resource references.
Configures a handler for resource subscription requests.
Typically paired with
WithUnsubscribeFromResourcesHandler.Configures a handler for resource unsubscription requests.
Configures a handler for logging level change requests from clients.
Filters
Configures message-level filters for the MCP server.
Configures request-specific filters for the MCP server.
Transport Configuration
Adds a server transport using standard input/output streams.Use Cases:
- Local server launched by a client process
- Command-line MCP servers
- Server exits when stdin is closed
Adds a server transport using custom input/output streams.Parameters:
inputStream: Stream to use as standard inputoutputStream: Stream to use as standard output
Complete Example
See Also
- McpServerServiceCollectionExtensions - Entry point with
AddMcpServer() - Server Attributes -
[McpServerTool],[McpServerPrompt],[McpServerResource] - Protocol Types - MCP protocol types including Tool, Prompt, and Resource