IT

iboughtbed/trpc-to-mcp

API development

이 페이지는 AI로 자동 번역되었습니다. 기술적인 세부 사항은 원문을 확인하세요. 원문 보기

36 stars 1 forks 품질 65 트렌드 20MIT

tRPC에서 MCP 패키지

개요

왜 tRPC 엔드포인트를 MCP 도구로 전환했는지 정확히 알고 있다면이 부분을 건너뛸 수 있습니다. 핵심에, Model Context Protocol (MCP)는 언어 모델은 잘 정의된 도구를 exposing하여 외부 시스템과 상호 작용하는 컨텍스트를 제공합니다. API endpoints를 감싸는 것은 아닙니다. AI가 선명하고 목적에 맞는 작업을 수행 할 수 있도록하는 것입니다. 이 라이브러리는 MCP 도구로 명시적으로 선택된 tRPC 절차만 변환하도록 만들어졌습니다. 이렇게하면 통제되는 AI 상호 작용을 위해 예정된 기능만 노출된다는 것을 보증합니다. MCP 도구로 효과적으로 변환 할 엔드 포인트를 기억하십시오. 명확한 설명, 의미있는 이름 및 잘 정의 된 입력 / 출력으로 거의 문서화되어야합니다. 이 주의깊은 디자인은 MCP 도구가 단순한 API 엔드포인트보다 더 중요하기 때문입니다. 그들은 언어 모델이 안정적으로 작동되는 컨텍스트를 형성합니다.

개요(AI 번역)

MCP 도구로 선택한 tRPC 절차를 켭니다.

  • tRPC v11 및 @modelcontextprotocol/server v2, MCP TypeScript SDK 사용
  • stdio 또는 HTTP를 통한 도구
  • 모든 도구 호출에 tRPC 컨텍스트 및 미들웨어 사용
  • 텍스트, 이미지, 기타 MCP 콘텐츠로 변환
  • 산출 schema를 가진 절차에서 구조화된 내용을 돌려보내십시오
  • 다음에 마운트.js 및 mcp 핸들러와 다른 프레임 워크

왜 당신이 선택하는 절차?

MCP 도구는 언어 모델이 스스로 호출하기로 결정하는 것입니다. 그래서 모델을 사용하려는 절차 만 노출. 모델은 이름, 설명 및 입력 스키마를 기반으로 도구를 선택합니다. 각 노출된 절차에 대해 명확한 이름, 그것을 사용할 때의 설명, 그리고 .describe() 각 입력 필드에.

설치

@trpc/server 11 및 @modelcontextprotocol/server 2는 피어 의존성입니다. mcp-handler] 2를 설치하면 mcp 핸들러 어댑터를 사용합니다. Node.js 20 이상이 필요합니다.

사용법

  1. tRPC 인스턴스에 McpMeta을 추가합니다.

  2. 도구로 원하는 절차에 mcp를 사용.

입력은 zod 4.와 같은 Standard JSON Schema를 구현하는 validator의 객체 스키마가 있어야 합니다.

이 페이지는 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 MCP 초보자라면? mcpskill.org 가이드부터 시작하세요 →