SharpMCP

Getting Started with SharpMCP

This guide will help you create your first MCP server using SharpMCP.

Installation

Install the complete SharpMCP package:

dotnet add package SharpMCP.Server

This automatically includes SharpMCP.Core and SharpMCP.Tools.Common.

Your First MCP Server

1. Create a New Project

dotnet new console -n MyMcpServer
cd MyMcpServer
dotnet add package SharpMCP.Server

2. Create a Simple Tool

using SharpMCP.Core.Tools;
using SharpMCP.Core.Schema;

[McpTool("greet", Description = "Greets a person with a custom message")]
public class GreetTool : McpToolBase<GreetArgs>
{
    public override string Name => "greet";
    public override string? Description => "Greets a person with a custom message";

    protected override Task<ToolResponse> ExecuteAsync(GreetArgs args, CancellationToken ct)
    {
        var message = $"Hello, {args.Name}! {args.Message}";
        return Task.FromResult(Success(message));
    }
}

public class GreetArgs
{
    [JsonRequired]
    [JsonDescription("Name of the person to greet")]
    public string Name { get; set; } = "";

    [JsonDescription("Custom greeting message")]
    public string Message { get; set; } = "Nice to meet you!";
}

3. Create the Server

using SharpMCP.Server;
using SharpMCP.Tools.Common;

class Program
{
    static async Task Main(string[] args)
    {
        var server = new McpServerBuilder()
            .WithName("MyMcpServer")
            .WithVersion("1.0.0")
            .AddTool(new GreetTool())
            .AddFileSystemTools() // Optional: add built-in file tools
            .Build();

        await server.RunAsync();
    }
}

4. Run Your Server

dotnet run

Your server will start and communicate via stdin/stdout using the MCP protocol.

Using Built-in Tools

SharpMCP includes comprehensive file system tools:

var server = new McpServerBuilder()
    .WithName("FileServer")
    .WithVersion("1.0.0")
    .AddFileSystemTools(allowedDirectories: [
        @"C:\MyProject", 
        @"C:\Data"
    ])
    .Build();

Available tools:

Testing Your Server

Manual Testing

  1. Run your server: dotnet run
  2. Send JSON-RPC messages to stdin
  3. Observe responses on stdout

Using the Test Harness

var server = new McpServerBuilder()
    .AddTool(new GreetTool())
    .Build();

var response = await server.ExecuteToolAsync("greet", new {
    name = "Alice",
    message = "Welcome!"
});

Console.WriteLine(response.Content[0].Text); // "Hello, Alice! Welcome!"

Next Steps