Token导航 LogoToken导航TokenDH.com
MCP Csharp Starter logo
AI代理stdio官方来源来源级核验

MCP Csharp Starter

MCP Server

@modelcontextprotocol/inspector

一个功能完整的Model Context Protocol(MCP)C#服务器模板,使用官方C# SDK,展示了所有主要的MCP功能,适用于快速开发和集成。

工具数

0

提示词数

0

GitHub Stars

0

资源数

0
服务器模板资源管理C#VS CodeVS Code

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

SamMorrowDrums

提供方

SamMorrowDrums

最后核验

2026/5/17 20:20

运行时

Node.js

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

命令预览

npx @modelcontextprotocol/inspector -- dotnet run -- --stdio

详细介绍

MCP C#启动器

![CI](https://github.com/SamMorrowDrums/mcp-csharp-starter/actions/workflows/ci.yml) ![.NET](https://dotnet.microsoft.com/) ![C#](https://docs.microsoft.com/en-us/dotnet/csharp/) ![License: MIT](https://opensource.org/licenses/MIT) ![MCP](https://modelcontextprotocol.io/)

使用官方csharpsdk的C#中功能完整的模型上下文协议(MCP)服务器模板。这个初学者演示了利用干净、惯用的C#代码的所有主要MCP功能。NET 8和依赖注入。

📚 文档

✨ 特性

类别功能描述
工具hello带注释的基本工具
get_weather返回结构化JSON的工具
ask_llm调用LLM采样的工具
long_task带有进度更新的工具
load_bonus_tool动态加载新工具
bonus_calculator计算器(动态加载)
资源info://about静态信息资源
file://example.md基于文件的标记资源
模板greeting://{name}个性化问候
data://items/{id}按ID查找数据
提示greet各种风格的问候
code_review重点领域代码审查

🚀 快速开始

先决条件

安装

# Clone the repository
git clone https://github.com/SamMorrowDrums/mcp-csharp-starter.git
cd mcp-csharp-starter

# Restore packages
dotnet restore

运行服务器

stdio传输 (地方发展):

dotnet run

HTTP传输 (用于远程/web部署):

dotnet run -- --http
# Or with custom port:
dotnet run -- --http --port 8080
# Server runs on http://localhost:3000 by default

🔧 VS代码集成

该项目包括用于无缝开发的VS代码配置:

  1. 在VS Code中打开项目
  2. MCP配置位于 .vscode/mcp.json
  3. 构建于 Ctrl+Shift+B (或 Cmd+Shift+B 在Mac上)
  4. 使用F5进行调试(两种传输的配置)
  5. 使用VS Code的MCP工具测试服务器

使用DevContainers

  1. 安装 开发容器扩展
  2. 打开命令面板:“开发容器:在容器中重新打开”
  3. 一切都是预先配置好的,随时可以使用!

📁 项目结构

.
├── Program.cs             # Main entry point (stdio/HTTP)
├── Tools/
│   └── AllTools.cs        # All tool definitions
├── Resources/
│   └── AllResources.cs    # All resource definitions
├── Prompts/
│   └── AllPrompts.cs      # All prompt definitions
├── .vscode/
│   ├── mcp.json           # MCP server configuration
│   ├── tasks.json         # Build/run tasks
│   ├── launch.json        # Debug configurations
│   └── extensions.json
├── .devcontainer/
│   └── devcontainer.json
├── McpCSharpStarter.csproj
├── global.json
└── appsettings.json

🛠️ 发展

# Development with live reload (recommended)
dotnet watch run

# Build
dotnet build

# Run tests
dotnet test

# Format code
dotnet format

# Clean
dotnet clean

# Publish for production
dotnet publish -c Release

实时重新加载

dotnet watch run 命令在开发过程中提供自动重建。 更改任何 .cs 文件将自动重建并重新启动服务器。

🔍 MCP检查员

MCP检查员 是测试和调试MCP服务器的重要开发工具。

运行检查器

npx @modelcontextprotocol/inspector -- dotnet run -- --stdio

检查员提供什么

  • 工具选项卡:列出并调用所有已注册的带有参数的工具
  • 资源选项卡:浏览和阅读资源和模板
  • 提示选项卡:查看和测试提示模板
  • 日志选项卡:请参阅客户端和服务器之间的JSON-RPC消息
  • 模式验证:验证工具输入/输出模式

调试提示

  1. 在连接IDE/客户端之前启动检查器
  2. 使用“日志”选项卡查看确切的请求/响应有效载荷
  3. 测试工具注释(ReadOnlyHint等)已正确公开
  4. 验证是否显示进度通知 long_task
  5. 检查McpServer注入是否适用于采样工具

📖 功能示例

具有属性的工具

[McpServerToolType]
public class GreetingTools
{
    [McpServerTool(Name = "hello", Title = "Say Hello")]
    [Description("A friendly greeting tool")]
    public static string Hello(
        [Description("The name to greet")] string name)
    {
        return $"Hello, {name}!";
    }
}

资源模板

[McpServerResourceType]
public class StaticResources
{
    [McpServerResource(
        UriTemplate = "greeting://{name}",
        Name = "Personalized Greeting",
        MimeType = "text/plain")]
    public static string Greeting(string name)
    {
        return $"Hello, {name}!";
    }
}

带取样的工具

[McpServerTool(Name = "ask_llm")]
public static async Task AskLlm(
    McpServer server,
    [Description("The prompt")] string prompt,
    CancellationToken cancellationToken)
{
    var result = await server.SampleAsync(
        new CreateMessageRequestParams
        {
            Messages = [
                new SamplingMessage
                {
                    Role = Role.User,
                    Content = [new TextContentBlock { Text = prompt }]
                }
            ],
            MaxTokens = 100
        },
        cancellationToken: cancellationToken);
    
    return result.Content.OfType()
        .FirstOrDefault()?.Text ?? "";
}

快速定义

[McpServerPromptType]
public class CodeReviewPrompts
{
    [McpServerPrompt(Name = "code_review", Title = "Code Review")]
    public static IEnumerable
 CodeReview(
        [Description("The code to review")] string code,
        [Description("Programming language")] string language)
    {
        return [
            new PromptMessage
            {
                Role = Role.User,
                Content = new TextContentBlock 
                { 
                    Text = $"Review this {language} code:\n```{language}\n{code}\n```" 
                }
            }
        ];
    }
}

🔐 配置

通过配置 appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    }
  }
}

🤝 贡献

欢迎投稿!请确保您的更改与其他语言初学者保持功能对等。

📄 许可证

MIT许可证-请参阅 许可证 了解详情。

目录标签

目录标签

服务器模板资源管理C#VS CodeMCP协议本地部署C#开发工具集成

支持客户端

VS Code

接入字段

传输方式(transport,传输协议)

stdio

鉴权方式(authType,认证方式)

none

运行时(runtime,运行环境)

Node.js

来源包(packageName,安装包名)

@modelcontextprotocol/inspector

工具数量(toolCount,工具数)

0

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

stdionone部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

来源信息

继续浏览同类 MCP