任务API服务器-MCP类型脚本实现
用TypeScript编写的用于任务管理API的模型上下文协议(MCP)实现。该项目既是参考实现,也是功能任务管理服务器。
概述
此MCP服务器连接到外部任务API服务,并为任务管理提供标准化接口。它支持两种运行时模式:
- STDIO模式:基于CLI的应用程序和AI代理的标准输入/输出通信
- HTTP+SSE模式:可通过Web访问的服务器,为浏览器和基于HTTP的客户端提供服务器发送事件
该服务器提供了一套完整的任务管理操作、广泛的验证和强大的错误处理。
特性
- 任务管理操作:
- 列出具有过滤功能的现有任务 - 创建具有可自定义属性的新任务 - 更新任务详细信息(描述、状态、类别、优先级) - 完成或不再需要时删除任务
- 双界面模式:
- STDIO协议支持命令行和AI代理集成 - HTTP+SSE协议,具有基于浏览器的web界面访问
- MCP协议实现:
- 模型上下文协议的完整实现 - 任务数据结构的资源 - 任务操作工具 - 错误处理和信息性消息
- 质量保证:
- 用于验证的综合测试客户端 - 测试完成后自动关闭服务器 - API响应的详细验证
入门指南
先决条件
- Node.js 16.x或更高版本
- npm或pnpm包管理器
安装
- 克隆存储库:
git clone https://github.com/yourusername/mcp-template-ts.git
cd mcp-template-ts- 安装依赖项:
npm install或使用pnpm:
pnpm install- 创建一个
.env使用您的任务API凭据的文件:
TASK_MANAGER_API_BASE_URL=https://your-task-api-url.com/api
TASK_MANAGER_API_KEY=your_api_key_here
TASK_MANAGER_HTTP_PORT=3000- 构建项目:
npm run build运行服务器
STDIO模式(用于CLI/AI集成)
npm start或
node dist/index.jsHTTP模式(用于web访问)
npm run start:http或
node dist/http-server.js默认情况下,HTTP服务器在端口3000上运行。您可以通过设置 TASK_MANAGER_HTTP_PORT 环境变量。
测试
运行综合测试套件以验证功能:
npm test这将:
- 构建项目
- 启动服务器实例
- 将测试客户端连接到服务器
- 执行所有任务操作
- 验证正确的回答
- 自动关闭服务器
使用MCP客户端
STDIO客户端
要从应用程序连接到STDIO服务器,请执行以下操作:
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import * as path from 'path';
// Create transport
const transport = new StdioClientTransport({
command: 'node',
args: [path.resolve('path/to/dist/index.js')]
});
// Initialize client
const client = new Client(
{
name: "your-client-name",
version: "1.0.0"
},
{
capabilities: {
prompts: {},
resources: {},
tools: {}
}
}
);
// Connect to server
await client.connect(transport);
// Example: List all tasks
const listTasksResult = await client.callTool({
name: "listTasks",
arguments: {}
});
// Example: Create a new task
const createTaskResult = await client.callTool({
name: "createTask",
arguments: {
task: "Complete project documentation",
category: "Documentation",
priority: "high"
}
});
// Clean up when done
await client.close();HTTP客户端
要从浏览器连接到HTTP服务器,请执行以下操作:
Task Manager
import { Client } from 'https://cdn.jsdelivr.net/npm/@modelcontextprotocol/sdk/dist/esm/client/index.js';
import { SSEClientTransport } from 'https://cdn.jsdelivr.net/npm/@modelcontextprotocol/sdk/dist/esm/client/sse.js';
document.addEventListener('DOMContentLoaded', async () => {
// Create transport
const transport = new SSEClientTransport('http://localhost:3000/mcp');
// Initialize client
const client = new Client(
{
name: "browser-client",
version: "1.0.0"
},
{
capabilities: {
prompts: {},
resources: {},
tools: {}
}
}
);
// Connect to server
await client.connect(transport);
// Now you can use client.callTool() for tasks
});
Task Manager
可用工具
列表Tasks
列出所有可用任务。
const result = await client.callTool({
name: "listTasks",
arguments: {
// Optional filters
status: "pending", // Filter by status
category: "Work", // Filter by category
priority: "high" // Filter by priority
}
});创建任务
创建新任务。
const result = await client.callTool({
name: "createTask",
arguments: {
task: "Complete the project report", // Required: task description
category: "Work", // Optional: task category
priority: "high" // Optional: low, medium, high
}
});updateTask
更新现有任务。
const result = await client.callTool({
name: "updateTask",
arguments: {
taskId: 123, // Required: ID of task to update
task: "Updated task description", // Optional: new description
status: "done", // Optional: pending, started, done
category: "Personal", // Optional: new category
priority: "medium" // Optional: low, medium, high
}
});删除任务
删除任务。
const result = await client.callTool({
name: "deleteTask",
arguments: {
taskId: 123 // Required: ID of task to delete
}
});环境变量
| 变量 | 描述 | 默认值 |
|---|---|---|
| TASK_MANAGER_API_BASE_URL | 外部任务API的URL | 无(必需) |
| TASK_MANAGER_API_KEY | 用于身份验证的API密钥 | 无(必需) |
| TASK_MANAGER_HTTP_PORT | HTTP服务器的端口 | 3000 |
| PORT | 备用端口名(优先) | 无 |
项目结构
mcp-template-ts/
├── dist/ # Compiled JavaScript files
├── src/ # TypeScript source files
│ ├── index.ts # STDIO server entry point
│ ├── http-server.ts # HTTP+SSE server entry point
│ ├── test-client.ts # Test client implementation
├── .env # Environment variables
├── package.json # Project dependencies
├── tsconfig.json # TypeScript configuration
└── README.md # Project documentation发展
- 在监视模式下启动TypeScript编译器:
npm run watch- 运行测试以验证更改:
npm test许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
致谢
- 该项目使用 @模型上下文协议/sdk 用于MCP协议实现
- 专为与AI工具和web应用程序集成而构建
