KM

kriasoft/mcp-client-gen

开发工具官方

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

21 stars 5 forks 质量 80 趋势 8MIT

只需几秒钟即可将任何MCP服务器转换为类型安全的TypeScript SDK——支持OAuth 2.1和多提供者支持

概览

为远程MCP服务器(可流式HTTP或SSE)生成一个类型化的TypeScript客户端:指向一个URL,获取一个包含每个工具一个方法的模块。 - — 输入和输出来自服务器的 JSON Schema - 每个方法调用官方 MCP SDK 并返回结果不变 - — 生成代码只导入 SDK 类型,从不导入该包 - 若服务器请求,则在浏览器中批准,生成继续 MCP SDK(如果服务器使用 OAuth,则 oauth-callback): 没有 OAuth?只用 SDK 连接:waitit client.connect(new StreamableHTTPClientTransport(new URL(url)))。写入文件后,CLI 会为你的服务器打印正确的片段。 工具是方法。结果是SDK的CallToolResult;当工具声明输出模式时,在你勾选isError后,会输入structuredContent: 提示和资源(当服务器拥有时)存在命名空间: 在测试中,传递一个纯对象而不是真实客户端:工厂只需要它调用的方法。

概述(AI 翻译)

为远程MCP服务器(可流式HTTP或SSE)生成一个类型化的TypeScript客户端:指向一个URL,获取一个包含每个工具一个方法的模块。

  • Typed — 输入和输出来自服务器的 JSON Schema
  • Thin — 每个方法调用官方MCP SDK并返回其结果不变
  • 无锁定 — 生成的代码只导入 SDK 类型,绝不会导入该包
  • OAuth 处理 — 如果服务器请求,浏览器中批准,生成继续

快速启动

**1.从服务器的 URL 生成一个模块:

2.安装MCP SDK(如果服务器使用OAuth,则安装oauth-callback):

3.连接一个SDK Client并封装:

没有OAuth?只用SDK连接:await client.connect(new StreamableHTTPClientTransport(new URL(url)))。写入文件后,CLI会打印适合服务器的正确片段。

使用客户端

工具是方法。结果是SDK的CallToolResult;当工具声明输出模式时,在你勾选isError后输入structuredContent:

提示和资源(当服务器拥有时)存在命名空间:

在测试中,传递一个普通对象而不是实 Client:工厂只需要它调用的方法。

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

README

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

MCP Client Generator

Generate a typed TypeScript client for a remote MCP server (Streamable HTTP or SSE): point it at a URL, get a module with one method per tool.

await notion.search({ query: "Meeting Notes" }); // instead of client.callTool({ name: "notion-search", arguments: … })
  • Typed — inputs and outputs come from the server’s JSON Schemas
  • Thin — each method calls the official MCP SDK and returns its result unchanged
  • No lock-in — generated code imports only SDK types, never this package
  • OAuth handled — if the server asks, approve in the browser and generation continues

Quick Start

1. Generate a module from the server’s URL:

npx mcp-client-gen https://mcp.notion.com/mcp -o src/notion.ts

2. Install the MCP SDK (and oauth-callback if the server uses OAuth):

npm install @modelcontextprotocol/client oauth-callback

3. Connect an SDK Client and wrap it:

import { Client } from "@modelcontextprotocol/client";
import { browserAuth } from "oauth-callback/mcp";
import { createNotionClient } from "./notion.js";

const client = new Client(
  { name: "my-app", version: "1.0.0" },
  { versionNegotiation: { mode: "auto" } }, // use the newest protocol the server speaks
);
const auth = browserAuth({
  serverUrl: "https://mcp.notion.com/mcp",
  redirectUri: "http://127.0.0.1:3000/callback",
  clientName: "my-app",
});
await auth.connect(client); // opens the browser when needed
await client.listTools(); // once: lets the SDK validate typed results

const notion = createNotionClient(client);
const result = await notion.search({ query: "Meeting Notes" });

No OAuth? Connect with the SDK alone: await client.connect(new StreamableHTTPClientTransport(new URL(url))). The CLI prints the right snippet for your server after writing the file.

Using the Client

Tools are methods. Results are the SDK’s CallToolResult; when a tool declares an output schema, structuredContent is typed once you’ve checked isError:

const result = await github.searchIssues({ query: "is:open" });
if (result.isError) throw new Error("search failed");
result.structuredContent.items; // typed

Prompts and resources, when the server has them, live in namespaces:

await github.prompts.summarizePr({ number: "42" });
await github.resources.read("repo://octo/app/README.md"); // any URI
await github.resources.issue({ owner: "octo", number: "42" }); // a URI template, filled in
在 GitHub 查看完整 README

安装

npx mcp-client-gen https://mcp.notion.com/mcp -o src/notion.ts
新手?先看 mcpskill.org 教程 →

配置

// .mcp.json { "mcpServers": { "notion": { "url": "https://mcp.notion.com/mcp" }, "github": { "url": "https://api.githubcopilot.com/mcp/", "headers": { "Authorization": "Bearer ${GITHUB_TOKEN}" }, }, }, }