IT

iboughtbed/trpc-to-mcp

API 开发

本页由 AI 翻译,技术细节请以原文为准。 查看原文

36 stars 1 forks 质量 65 趋势 20MIT

tRPC 到 MCP 包

概览

如果你清楚为什么要把 tRPC 端点变成 MCP 工具,可以跳过这部分。 模型上下文协议(MCP)的核心是通过公开定义明确的工具,为语言模型提供与外部系统交互的上下文。这不仅仅是包装API端点——而是让AI能够清晰且有目的地执行任务。 该库旨在仅将您明确选择的 tRPC 过程转换为 MCP 工具。通过这样做,它确保只暴露用于受控 AI 交互的功能,而非无差别地转换所有端点。 请记住,要让端点有效转变为MCP工具,必须有详细的文档,包含清晰的描述、有意义的名称和明确的输入/输出。这种精心设计至关重要,因为MCP工具不仅仅是API端点——它们构成了使语言模型能够可靠运行的操作环境。

概述(AI 翻译)

将你选择的tRPC程序转化为MCP工具。

  • 可兼容 tRPC v11 和 @modelcontextprotocol/server v2,MCP TypeScript SDK
  • 通过标准或HTTP提供工具
  • 在每次工具调用中使用 tRPC 上下文和中间件
  • 将输出转换为文本、图片或其他MCP内容
  • 返回带有输出模式的过程结构化内容
  • 通过 mcp handler 挂载于 Next.js 和其他框架

为什么只选你自己选择的手术?

MCP工具是语言模型自行决定调用的,所以只暴露你希望模型使用的过程。模型根据工具名称、描述和输入模式选择工具。给每个暴露过程一个清晰的名称,描述它的功能和使用时间,并在每个输入字段上标注.describe()。

安装

@trpc/server 11 和 @modelcontextprotocol/server 2 是对等依赖。只有在使用 mcp 处理适配器时才安装 mcp-handler 2。Node.js 20 或更高版本则需要。

用途

  1. 将McpMeta添加到你的tRPC实例中。

  2. 在你想用作工具的程序上启用mcp。

输入必须是来自实现标准 JSON 模式的验证器的对象模式,例如 zod 4。

本页由 AI 翻译,技术细节请以原文为准。

README

以下为原文 README 节选,完整内容请查看原文。

trpc-to-mcp

Turn the tRPC procedures you choose into MCP tools.

Install

pnpm add trpc-to-mcp @modelcontextprotocol/server

@trpc/server 11 and @modelcontextprotocol/server 2 are peer dependencies. Install mcp-handler 2 only if you use the mcp-handler adapter. Node.js 20 or later is required.

Usage

  1. Add McpMeta to your tRPC instance.
import { initTRPC } from "@trpc/server";
import { type McpMeta } from "trpc-to-mcp";

const t = initTRPC.context().meta().create();
  1. Enable mcp on the procedures you want as tools.
import { z } from "zod";

export const appRouter = t.router({
  hello: t.procedure
    .meta({
      mcp: {
        enabled: true,
        description: "Say hello to someone",
        // Optional. Defaults to the procedure path with dots replaced by
        // underscores, e.g. "hello" or "user_byId".
        name: "say_hello",
      },
    })
    .input(z.object({ name: z.string().describe("Who to greet") }))
    .query(({ input }) => `Hello ${input.name}`),
});

Inputs must be object schemas from a validator that implements Standard JSON Schema, such as zod 4.2 or later, ArkType, or Valibot with toStandardJsonSchema(). Chained .input() calls are merged into one tool input schema. Subscriptions can’t be tools.

tRPC validates input on every call, so your zod transforms and refinements run as usual. Errors thrown by a procedure, including TRPCError, come back to the client as a tool result with isError: true.

meta.mcp also takes title and annotations. Queries get readOnlyHint: true unless you set it yourself.

Serve your tools

createMcpServer returns an McpServer with your tools registered. You can register more tools, prompts, and resources on it.

Over stdio:

import { serveStdio } from "@modelcontextprotocol/server/stdio";
import { createMcpServer } from "trpc-to-mcp";

serveStdio(() =>
  createMcpServer({ name: "my-app", version: "1.0.0" }, appRouter, { db }),
);
在 GitHub 查看完整 README

安装

This server does not publish a one-line install command.

Open the repository installation guide 新手?先看 mcpskill.org 教程 →