🪶 mcp灯
一个零依赖、TypeScript优先的框架,用于构建和使用模型上下文协议(MCP)服务器,开销最小。
🚀 为什么选择mcp灯?
mcp-light使开发人员能够轻松地将任何TypeScript或Bun后端转换为符合mcp的服务器,AI模型和代理可以本地调用。
它处理:
- ✅ JSON-RPC 2.0传输
- ✅ 自动工具发现(
/mcp/introspect) - ✅ 批量请求
- ✅ 可选的身份验证挂钩
- ✅ TypeScript原生DX
- ✅ 零依赖
它很快,很薄,很专注。
📦 安装
bun add mcp-light或
npm install mcp-light🧩 示例
创建HTTP MCP服务器
import { createMCPServer, tool } from "mcp-light";
import { serveWithBun } from "mcp-light/adapters/bun";
const mcp = createMCPServer({ name: "example-crm" });
mcp.register(
tool("getLeads", () => [
{ name: "Acme Corp", stage: "Negotiation" },
{ name: "Globex", stage: "Closed Won" },
])
);
serveWithBun(mcp, 3001);创建一个stdio MCP服务器(用于Claude Desktop)
import { createMCPServer, tool } from "mcp-light";
import { serveWithStdio } from "mcp-light/adapters/stdio";
const mcp = createMCPServer({ name: "example-crm" });
mcp.register(
tool("getLeads", () => [
{ name: "Acme Corp", stage: "Negotiation" },
{ name: "Globex", stage: "Closed Won" },
])
);
serveWithStdio(mcp);从客户那里调用它
import { createMCPClient } from "mcp-light";
const crm = createMCPClient("http://localhost:3001/mcp");
const leads = await crm.call("getLeads", {});
console.log(leads);🔍 内省输出
每个mcp-light服务器都会自动公开其模式:
GET /mcp/introspect{
"name": "example-crm",
"version": "0.1.0",
"tools": [{ "name": "getLeads", "description": "Fetch all CRM leads" }]
}⚙️ 特性
| 特性 | 描述 |
|---|---|
| 轻量级 | 零运行时依赖关系——为Bun和Node构建 |
| 符合MCP标准 | 遵循官方的模型上下文协议语义 |
| 可内省的 | /mcp/introspect LLM和代理的端点 |
| 批处理就绪 | 自动处理阵列有效载荷 |
| 可扩展 | 轻松添加身份验证、验证或模式元数据 |
🔌 连接到克劳德桌面和OpenAI
克劳德桌面
使用stdio适配器,连接到Claude Desktop很简单:
- 创建您的stdio服务器 (见上面的例子,使用
serveWithStdio)
- 配置Claude桌面 通过编辑其配置文件:
- macOS: ~/Library/Application Support/Claude/claude_desktop_config.json - 视窗: %APPDATA%\Claude\claude_desktop_config.json - Linux: ~/.config/Claude/claude_desktop_config.json
- 添加服务器配置:
{
"mcpServers": {
"mcp-light-server": {
"command": "bun",
"args": ["run", "path/to/your-stdio-server.ts"]
}
}
}- 重新启动克劳德桌面 --您的工具现在将出现在Claude的工具选择中!
OpenAI产品
OpenAI助手API
使用您的mcp-light服务器作为OpenAI Assistants的功能工具:
import { createMCPClient } from "mcp-light";
import OpenAI from "openai";
const mcp = createMCPClient("http://localhost:3001/mcp");
// Get available tools
const introspection = await mcp.introspect();
// Convert to OpenAI function format
// Note: You may need to define schemas separately if using OpenAI
const functions = introspection.tools.map((tool) => ({
name: tool.name,
description: tool.description || "",
parameters: { type: "object", properties: {} }, // Define your schema here
}));
const openai = new OpenAI();
const assistant = await openai.beta.assistants.create({
model: "gpt-4",
tools: functions.map((f) => ({ type: "function", function: f })),
});
// When calling the assistant, proxy tool calls to your MCP server
const callTool = async (name: string, args: any) => {
return await mcp.call(name, args);
};OpenAI函数调用(聊天完成)
对于标准的OpenAI函数调用:
import { createMCPClient } from "mcp-light";
import OpenAI from "openai";
const mcp = createMCPClient("http://localhost:3001/mcp");
const openai = new OpenAI();
// Get your MCP tools
const introspection = await mcp.introspect();
// Convert to OpenAI format
// Note: You may need to define schemas separately if using OpenAI
const functions = introspection.tools.map((tool) => ({
name: tool.name,
description: tool.description || "",
parameters: { type: "object", properties: {} }, // Define your schema here
}));
// Use in chat completion
const completion = await openai.chat.completions.create({
model: "gpt-4",
messages: [{ role: "user", content: "Get me all leads" }],
tools: functions.map((f) => ({ type: "function", function: f })),
});
// Handle tool calls by forwarding to your MCP server
if (completion.choices[0].message.tool_calls) {
for (const call of completion.choices[0].message.tool_calls) {
const result = await mcp.call(
call.function.name,
JSON.parse(call.function.arguments)
);
// Use result in subsequent API calls
}
}测试您的连接
验证您的HTTP服务器是否可访问:
# Check introspection endpoint
curl http://localhost:3001/mcp/introspect
# Test a tool call
curl -X POST http://localhost:3001/mcp \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"getLeads","params":{},"id":1}'🧠 为什么重要
如果您正在构建CRM、分析API或交易代理后端,您可以在几分钟内将您的功能公开为AI收费工具。
代理和LLM可以在没有自定义SDK或OpenAPI模式的情况下自检和调用您的端点。
🧪 发展
bun install
bun run dev # runs example server
bun test # run built-in Bun tests
bun run build # build for distribution📝 许可证
麻省理工学院©2025乔·托莱达诺
______________________________________________________________________
mcp很轻——因为模型上下文协议不应该很重。
