AI SDK工具转MCP
 
概述
这是一个小型工具,用于将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的接口:
- 模式/架构直接传递Zod模式(无需转换 - 两者都使用Zod!)
- 执行封装AI软件开发工具包(SDK)
execute提供其期望的上下文对象的函数 - 错误处理捕获错误并用有用的信息进行包装
- 结果格式化将结果转换为 FastMCP 所期望的 JSON 字符串
示例
看看这个 例子 目录:
- basic-stdio.ts 翻译为中文是:“基本标准输入输出文件(TypeScript)”。不过,这里的“basic-stdio.ts”可能是一个具体的文件名或项目名,直接翻译可能不完全准确,因为它可能结合了特定的上下文或项目命名习惯。在编程或软件开发领域,这样的文件名通常表示这是一个用TypeScript编写的、处理基本标准输入输出功能的文件 - 基本的stdio服务器示例
- server.ts(服务器配置文件/服务器类型脚本文件) - 配备多种工具的完整服务器
使用以下命令运行示例:
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 许可证 - 请参阅 许可证 文件中有详细信息
相关的
- AI SDK(人工智能软件开发工具包) - Vercel的AI软件开发工具包,用于构建AI驱动的应用程序
- FastMCP - 快速、简单的MCP服务器框架
- 模型上下文协议 - 连接AI模型与外部上下文的协议
