文学
用于构建MCP(模型上下文协议)服务器的TypeScript框架
弃用通知: 自从 官方SDK 已经提供了类似于此项目的高级API,此项目将不再维护。 如果您喜欢此项目中的CLI命令,可以独立使用它们:https://github.com/wong2/mcp-cli
特性
安装
npm install litemcp zod快速启动
import { LiteMCP } from "litemcp";
import { z } from "zod";
const server = new LiteMCP("demo", "1.0.0");
server.addTool({
name: "add",
description: "Add two numbers",
parameters: z.object({
a: z.number(),
b: z.number(),
}),
execute: async (args) => {
return args.a + args.b;
},
});
server.addResource({
uri: "file:///logs/app.log",
name: "Application Logs",
mimeType: "text/plain",
async load() {
return {
text: "Example log content",
};
},
});
server.start();您可以使用以下命令在终端中测试服务器:
npx litemcp dev server.js核心概念
工具
MCP中的工具允许服务器公开可执行函数,这些函数可由客户端调用,并由LLM用于执行操作。
server.addTool({
name: "fetch",
description: "Fetch the content of a url",
parameters: z.object({
url: z.string(),
}),
execute: async (args) => {
const content = await fetchWebpageContent(args.url);
return content;
},
});资源
资源表示MCP服务器希望提供给客户端的任何类型的数据。这可能包括:
- 文件内容
- 屏幕截图和图像
- 日志文件
- 还有更多
每个资源都由一个唯一的URI标识,可以包含文本或二进制数据。
server.addResource({
uri: "file:///logs/app.log",
name: "Application Logs",
mimeType: "text/plain",
async load() {
return {
text: await readLogFile(),
};
},
});您还可以在中返回二进制内容 load:
async load() {
return {
blob: 'base64-encoded-data'
}
}提示
提示使服务器能够定义可重用的提示模板和工作流,客户端可以轻松地向用户和LLM展示。它们提供了一种强大的方法来标准化和共享常见的LLM交互。
server.addPrompt({
name: "git-commit",
description: "Generate a Git commit message",
arguments: [
{
name: "changes",
description: "Git diff or description of changes",
required: true,
},
],
load: async (args) => {
return `Generate a concise but descriptive commit message for these changes:\n\n${args.changes}`;
},
});日志记录
您可以通过以下方式向客户端发送日志消息 server.logger
server.addTool({
name: "download",
description: "Download a file from a url",
parameters: z.object({
url: z.string(),
}),
execute: async (args) => {
server.logger.info("Downloading file", { url: args.url });
// ...
server.logger.info("Downloaded file", { url: args.url });
return response;
},
});这 logger 对象具有以下方法:
debug(message: string, context?: JsonValue)info(message: string, context?: JsonValue)warn(message: string, context?: JsonValue)error(message: string, context?: JsonValue)
运行服务器
调试与 mcp-cli
测试和调试服务器的最快方法是 mcp-cli:
npx litemcp dev server.js
npx litemcp dev server.ts // ts files are also supported这将运行您的服务器 mcp-cli 用于在终端中测试和调试MCP服务器。
检查 MCP Inspector
另一种方式是使用官方 MCP Inspector 使用Web UI检查服务器:
npx litemcp inspect server.js苏格兰和南方能源公司运输
服务器正在运行 stdio 默认运输。您还可以使用SSE模式运行服务器:
server.start({
transportType: "sse",
sse: {
endpoint: "/sse",
port: 8080,
},
});这将启动服务器并侦听上的SSE连接http://localhost:8080/sse.
然后,您可以通过以下方式连接到服务器 SSE运输 在客户端。
展示
如果您使用LiteMCP开发了服务器,请在此处提交PR进行展示!
- https://github.com/wong2/mcp-jina-reader
- https://github.com/nloui/paperless-mcp
路线图
- 添加对资源模板的支持
相关
- mcp-cli -用于测试和调试MCP服务器的CLI
- mcpservers.org -精心策划的MCP服务器列表
- FastMCP -用于MCP服务器开发的Python库,本项目的灵感来源
