MCPSharp Documentation

repository·master·Indexed 18 days ago

https://github.com/afrise/mcpsharp

A .NET library for creating Model Context Protocol (MCP) servers and clients. MCPSharp enables developers to expose .NET methods as AI-discoverable tools using attributes like [McpTool], integrate with Microsoft.Extensions.AI and Semantic Kernel, and manage server lifecycles via MCPServer and MCPClient.

Tokens
1.7K
Snippets
9
Records
9
Agent score
13%

What's inside MCPSharp

  1. Define an MCP Tool using [McpTool]

    master

    To expose a .NET method as an MCP tool, create a class and decorate the method with the [McpTool] attribute. You can also use [McpParameter] to provide metadata like descriptions and requirement status for individual parameters. Note that [McpFunction] is deprecated; use [McpTool] instead.

    using MCPSharp;
    
    public class Calculator
    {
        [McpTool("add", "Adds two numbers")]
        public static int Add([McpParameter(true)] int a, [McpParameter(true)] int b)
        {
            return a + b;
        }
    }
  2. Integrate Semantic Kernel functions with MCPSharp

    master

    To expose Semantic Kernel functions as MCP tools, decorate your class methods with the [KernelFunction] attribute and then explicitly register the class with the MCP server using MCPServer.Register<T>().

    using Microsoft.SemanticKernel;
    
    public class MySkillClass
    {
        [KernelFunction("MyFunction")]
        [Description("Description of my function")]
        public string MyFunction(string input) => $"Processed: {input}$";
    }
    
    // Register with MCPServer
    MCPServer.Register<MySkillClass>();
  3. Enable XML Documentation for MCP Tools

    master

    MCPSharp can automatically extract tool descriptions and parameter metadata from your C# XML comments. To enable this, you must set <GenerateDocumentationFile>true</GenerateDocumentationFile> in your project file.

    <PropertyGroup>
        <GenerateDocumentationFile>true</GenerateDocumentationFile>
        <NoWarn>$(NoWarn);1591</NoWarn>
    </PropertyGroup>
  4. Integrate MCP tools with Microsoft.Extensions.AI

    master

    You can connect to an MCP server using MCPClient and convert its tools into AIFunction objects. This allows you to plug MCP tools directly into the ChatOptions.Tools property of any IChatClient implementation.

    // Client-side integration
    MCPClient client = new("AIClient", "1.0", "path/to/mcp/server");
    IList<AIFunction> functions = await client.GetFunctionsAsync();
  5. Register tools dynamically with AddToolHandler

    master

    If you need to register tools at runtime with custom implementation logic rather than using attributes, use MCPServer.AddToolHandler. This requires defining a Tool object with a name, description, and InputSchema, along with a delegate that handles the execution.

    MCPServer.AddToolHandler(new Tool() 
    {
        Name = "dynamicTool",
        Description = "A dynamic tool",
        InputSchema = new InputSchema {
            Type = "object",
            Required = ["input"],
            Properties = new Dictionary<string, ParameterSchema>{
                {"input", new ParameterSchema{Type="string", Description="Input value"}}
            }
        }
    }, (string input) => { return $"You provided: {input}"; });
  6. Start an MCP Server

    master

    Use MCPServer.StartAsync(string serverName, string version) to start the server. By default, it automatically discovers methods marked with [McpTool] in the base assembly. To register tools from a different library or class, use MCPServer.Register<T>().

    await MCPServer.StartAsync("CalculatorServer", "1.0.0");
  7. Reference: MCPSharp Attributes

    master

    Attributes used to define MCP capabilities on classes and methods.

    // [McpTool]
    // Marks a class or method as an MCP tool.
    // Parameters:
    // - Name: The tool name (default: class/method name)
    // - Description: Description of the tool
    
    // [McpParameter]
    // Provides metadata for function parameters.
    // Parameters:
    // - Description: Parameter description
    // - Required: Whether the parameter is required (default: false)
    
    // [McpResource]
    // Marks a property or method as an MCP resource.
    // Parameters:
    // - Name: Resource name
    // - Uri: Resource URI (can include templates)
    // - MimeType: MIME type of the resource
    // - Description: Resource description
  8. Reference: MCPSharp Server and Client Methods

    master

    Core methods for managing the MCP server lifecycle and interacting with MCP clients.

    // Server Methods
    // MCPServer.StartAsync(string serverName, string version)
    // MCPServer.Register<T>()
    // MCPServer.AddToolHandler(Tool tool, Delegate func)
    
    // Client Methods
    // new MCPClient(string name, string version, string server, string args = null, IDictionary<string, string> env = null)
    // client.GetToolsAsync()
    // client.CallToolAsync(string name, Dictionary<string, object> parameters)
    // client.GetResourcesAsync()
    // client.GetFunctionsAsync()