Token导航 LogoToken导航TokenDH.com
AI SDK Tool To MCP logo
开发工具未说明官方级别未说明来源级核验

AI SDK Tool To MCP

MCP Server

一个将AI SDK工具转换为FastMCP工具格式的轻量级实用程序,支持TypeScript类型安全和错误处理。

工具数

0

提示词数

0

GitHub Stars

0

资源数

0
错误处理TypeScriptClaudeAI开发Claude DesktopClaudeCline

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

tengis617

提供方

tengis617

最后核验

2026/5/17 20:22

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

详细介绍

AI SDK工具转MCP

![License: MIT](https://opensource.org/licenses/MIT) ![TypeScript](https://www.typescriptlang.org/)

概述

这是一个小型工具,用于将AI SDK工具定义转换为FastMCP工具格式:

  • ✨(闪亮的星星或表示闪耀、光芒四射) 简单的包装函数 - 只有一个功能: toFastMCPTool()
  • 🔄(循环、重复的符号,可表示循环、重复的动作或过程) 保持类型安全 支持完整的TypeScript功能
  • 🛠️(扳手或工具的符号,常用于表示修理、工具或手动操作) 优雅地处理错误 带有自动错误包装功能
  • 📦 箱子/包裹 零依赖 - 仅包含您已有的同级依赖项

安装

bun add @tengis617/ai-sdk-tool-to-mcp
# or
npm install @tengis617/ai-sdk-tool-to-mcp

同级依赖项(如尚未安装,请进行安装):

bun add ai zod fastmcp

快速入门

import { tool } from 'ai';
import { z } from 'zod';
import { FastMCP } from 'fastmcp';
import { toFastMCPTool } from '@tengis617/ai-sdk-tool-to-mcp';

// Define your AI SDK tool
const weatherTool = tool({
  description: 'Get the current weather in a given location',
  inputSchema: z.object({
    location: z.string().describe('The city and state, e.g. San Francisco, CA'),
    unit: z.enum(['celsius', 'fahrenheit']).optional().default('fahrenheit'),
  }),
  execute: async ({ location, unit }) => {
    return { location, temperature: 72, unit, conditions: 'Sunny' };
  },
});

// Convert to FastMCP tool and add to server
const server = new FastMCP({ name: 'my-server', version: '1.0.0' });
server.addTool(toFastMCPTool('weather', weatherTool));

server.start({ transportType: 'stdio' });

使用方法

基本示例

import { tool } from 'ai';
import { z } from 'zod';
import { FastMCP } from 'fastmcp';
import { toFastMCPTool } from '@tengis617/ai-sdk-tool-to-mcp';

const calculatorTool = tool({
  description: 'Perform basic arithmetic operations',
  inputSchema: z.object({
    operation: z.enum(['add', 'subtract', 'multiply', 'divide']),
    a: z.number(),
    b: z.number(),
  }),
  execute: async ({ operation, a, b }) => {
    switch (operation) {
      case 'add': return { result: a + b };
      case 'subtract': return { result: a - b };
      case 'multiply': return { result: a * b };
      case 'divide': 
        if (b === 0) throw new Error('Division by zero');
        return { result: a / b };
    }
  },
});

const server = new FastMCP({
  name: 'calculator-server',
  version: '1.0.0',
});

// Convert and add the tool
server.addTool(toFastMCPTool('calculator', calculatorTool));

server.start({ transportType: 'stdio' });

多种工具

为您的服务器添加多个工具:

const server = new FastMCP({
  name: 'my-tools',
  version: '1.0.0',
});

server.addTool(toFastMCPTool('weather', weatherTool));
server.addTool(toFastMCPTool('calculator', calculatorTool));
server.addTool(toFastMCPTool('search', searchTool));

server.start({ transportType: 'stdio' });

复杂模式

封装器自动处理复杂的Zod模式:

const createUserTool = tool({
  description: 'Create a new user',
  inputSchema: z.object({
    username: z.string().min(3).max(20),
    profile: z.object({
      firstName: z.string(),
      lastName: z.string(),
      email: z.string().email(),
      age: z.number().optional(),
    }),
    tags: z.array(z.string()).default([]),
  }),
  execute: async (params) => {
    return {
      id: crypto.randomUUID(),
      ...params,
      createdAt: new Date().toISOString(),
    };
  },
});

server.addTool(toFastMCPTool('createUser', createUserTool));

API 参考文档

toFastMCPTool(name: string, aiTool: AISDKTool): FastMCPTool

将一个AI SDK工具转换为FastMCP工具。

参数:

  • name (字符串):工具的名称
  • aiTool (AISDKTool): 一个使用(某种方式或框架)创建的AI软件开发工具包(SDK)工具对象 tool() 来自……的 ai 包(或包裹)

返回值: 一个FastMCP工具对象,可以传递给 server.addTool()

它的功能是:

  • 通过Zod模式校验(AI SDK和FastMCP均原生支持Zod)
  • 封装AI软件开发工具包(SDK) execute 函数以配合 FastMCP 的调用约定工作
  • 处理错误并用有用的提示信息进行封装
  • 将结果转换为FastMCP所需的JSON字符串

它是如何工作的

这是一个简单的封装器,用于将AI SDK工具适配到FastMCP的接口:

  1. 模式/架构直接传递Zod模式(无需转换 - 两者都使用Zod!)
  2. 执行封装AI软件开发工具包(SDK) execute 提供其期望的上下文对象的函数
  3. 错误处理捕获错误并用有用的信息进行包装
  4. 结果格式化将结果转换为 FastMCP 所期望的 JSON 字符串

示例

看看这个 例子 目录:

使用以下命令运行示例:

bun run examples/basic-stdio.ts
bun run examples/server.ts

用例

  • 展示AI SDK工具 给MCP客户端(如Claude Desktop、Cline等)
  • 快速MCP服务器将您现有的AI SDK工具转换为MCP服务器
  • 工具共享在AI SDK和FastMCP应用程序中使用相同的工具定义

做出贡献

欢迎投稿!详情请见 \CONTRIBUTING.md\ 翻译成中文是:“贡献指南文件(Markdown 格式)” 作为指南。

许可证

MIT 许可证 - 请参阅 许可证 文件中有详细信息

相关的

目录标签

目录标签

错误处理TypeScriptClaudeAI开发工具转换本地部署SDK集成

支持客户端

Claude DesktopClaudeCline

接入字段

传输方式(transport,传输协议)

未说明

鉴权方式(authType,认证方式)

none

工具数量(toolCount,工具数)

0

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

未说明none部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

仍需确认:installCommand

来源信息

继续浏览同类 MCP