SuperAPI AI 工具集

mcp-server-skills

智能体与模型

查看项目

更新于 10-15 15:12

技能介绍

用途

mcp-server-skills 提供了一套在 Next.js(App Router)中构建 MCP 服务器的工程模式。其目标是把 MCP 工具的定义、输入校验和业务逻辑集中管理,让 MCP 处理器与 Web 端 server actions 复用同一份代码,避免逻辑重复与响应不一致。

主要能力

  • 使用 mcp-handler 创建 MCP 处理器,支持 HTTP/SSE 传输,stdio 可按需另行添加。
  • 在 lib/* 中定义共享的 Zod schema、工具定义与纯逻辑,作为单一数据源。
  • 通过 server actions 复用同一逻辑与 schema,同时驱动 Web UI 与测试。
  • 统一 { success, result | error } 的响应结构,使工具与 UI 返回保持一致。
  • 提供 Claude Desktop 通过 mcp-remote 桥接本地 MCP 服务的配置示例。

使用方式

推荐的目录结构:

app/
  api/[transport]/route.ts   # 一个处理器覆盖所有传输(如 /api/mcp)
  actions/mcp-actions.ts     # 复用相同逻辑/schema 的 server actions
lib/
  dice.ts | tools.ts         # Zod schema、工具定义、纯逻辑
components/                  # 调用 server actions 进行 Web 测试的 UI

共享 Zod schema 与工具定义(lib/dice.ts):

import { z } from "zod";

export const diceSchema = z.number().int().min(2);

export function rollDice(sides: number) {
  const validated = diceSchema.parse(sides);
  const value = 1 + Math.floor(Math.random() * validated);
  return { type: "text" as const, text: `🎲 You rolled a ${value}!` };
}

export const rollDiceTool = {
  name: "roll_dice",
  description: "Rolls an N-sided die",
  schema: { sides: diceSchema },
} as const;

可复用的 server actions(app/actions/mcp-actions.ts):

"use server";
import { rollDice as rollDiceCore, rollDiceTool } from "@/lib/dice";

export async function rollDice(sides: number) {
  try {
    const result = rollDiceCore(sides);
    return { success: true, result: { content: [result] } };
  } catch {
    return {
      success: false,
      error: { code: -32602, message: "Invalid parameters: sides must be >= 2" },
    };
  }
}

export async function listTools() {
  return {
    success: true,
    result: {
      tools: [
        {
          name: rollDiceTool.name,
          description: rollDiceTool.description,
          inputSchema: {
            type: "object",
            properties: { sides: { type: "number", minimum: 2 } },
            required: ["sides"],
          },
        },
      ],
    },
  };
}

轻量 MCP 路由(app/api/[transport]/route.ts):

import { createMcpHandler } from "mcp-handler";
import { rollDice, rollDiceTool } from "@/lib/dice";

const handler = createMcpHandler(
  (server) => {
    server.tool(
      rollDiceTool.name,
      rollDiceTool.description,
      rollDiceTool.schema,
      async ({ sides }) => ({ content: [rollDice(sides)] }),
    );
  },
  {}, // server options
  {
    basePath: "/api",     // must match folder path
    maxDuration: 60,
    verboseLogs: true,
  },
);

export { handler as GET, handler as POST };

Claude Desktop 配置(mcp-remote):

{
  "mcpServers": {
    "rolldice": {
      "command": "npx",
      "args": ["-y", "mcp-remote", "http://localhost:3000/api/mcp"]
    }
  }
}

适用场景

  • 在 Next.js 项目中为 AI 客户端(如 Claude Desktop)暴露 MCP 工具。
  • 希望 MCP 工具与 Web UI 共用同一套 schema 与业务逻辑,减少重复实现。
  • 需要同时支持 HTTP/SSE 传输,并可能后续补充 stdio 入口。
  • 面向 Vercel 等无状态部署环境构建 MCP 服务。

限制

  • 资料未提供具体的安装命令、依赖版本、收费或价格信息。
  • 资料未提供除 Roll Dice 之外的完整示例或生产环境注意事项。
  • 资料未说明 stdio 传输的具体实现方式,仅提到可按需单独添加。
  • 资料未提供性能、并发或安全方面的具体说明。