Skip to content

Echo 服务器示例 ​

一个极简的 MCP 服务器,演示工具、资源和提示词。

源代码:examples/EchoServer/

特性 ​

  • 工具:echo — 回显输入文本
  • 资源:echo://static — 返回存储的消息
  • 资源模板:echo://{text} — 回显 URI 参数
  • 提示词:capitalize — 将输入文本转换为大写

运行 ​

bash
cmake --preset debug -DMCP_BUILD_EXAMPLES=ON
cmake --build --preset debug
build/debug/examples/EchoServer/EchoServer

关键代码 ​

cpp
#include <mcp/server/McpServer.hpp>
#include <mcp/transport/StdioServerTransport.hpp>

#include <iostream>
#include <memory>
#include <string>

using namespace mcp;
using Ctx = RequestContext<CallToolRequestParams>;

int main() {
    auto transport = std::make_unique<StdioServerTransport>();

    ServerOptions opts;
    opts.server_info = Implementation{"EchoServer", "1.0.0"};
    opts.server_instructions = "An echo server — sends back what you send.";

    auto server = McpServer::Create(std::move(transport), opts);

    // 工具 — 回显输入文本
    server->RegisterTool("echo",
        ToolOptions{}.Description("Echo the input text back"),
        std::function<CallToolResult(const Ctx&)>(
            [](const Ctx& ctx) -> CallToolResult {
                auto& params = ctx.Params();
                std::string text;
                if (params.arguments && params.arguments->Contains("text")) {
                    text = (*params.arguments)["text"].GetString();
                }
                CallToolResult result;
                result.content.push_back(TextContent{"text", text});
                return result;
            }));

    // 静态资源
    server->RegisterResource("echo-static", "echo://static",
        ResourceOptions{}.Description("Static echo resource"),
        [](const std::string& uri) -> ReadResourceResult {
            TextResourceContents trc;
            trc.uri = uri;
            trc.text = "Echo!";
            ReadResourceResult rr;
            rr.contents = {ResourceContents{trc}};
            return rr;
        });

    // 模板资源 — 回显 URI 参数
    server->RegisterResourceTemplate("echo-template",
        "echo://{text}",
        ResourceOptions{}.Description("Echo the URI parameter"),
        [](const std::string& uri,
           const std::map<std::string, std::string>& vars) -> ReadResourceResult {
            TextResourceContents trc;
            trc.uri = uri;
            trc.text = "Echo: " + vars.at("text");
            ReadResourceResult rr;
            rr.contents = {ResourceContents{trc}};
            return rr;
        });

    // 提示词 — 将输入文本转换为大写
    server->RegisterPrompt("capitalize",
        PromptOptions{}.Description("Capitalize the input text"),
        [](const std::string& name,
           const std::optional<JsonValue>& args) -> GetPromptResult {
            (void)name;
            std::string text;
            if (args && args->Contains("text")) {
                text = (*args)["text"].GetString();
            }
            for (auto& c : text) c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
            GetPromptResult r;
            PromptMessage pm;
            pm.role = "user";
            pm.content = TextContent{"text", text};
            r.messages = {std::move(pm)};
            return r;
        });

    std::cerr << "EchoServer starting on stdio..." << std::endl;
    server->Run();
    return 0;
}