**Task-MCP 任务管理服务器**
一种模型上下文协议的实现,提供任务管理的标准接口,同时支持用于命令行/AI应用程序的STDIO模式和用于基于浏览器的客户端的HTTP+SSE模式。
服务介绍
Task API Server - MCP TypeScript 实现
这是一个用 TypeScript 编写的用于任务管理 API 的 Model Context Protocol (MCP) 实现。该项目既是一个参考实现,也是一个功能性的任务管理服务器。
概览
此 MCP 服务器连接到一个外部的任务 API 服务,并提供了一个标准化的任务管理接口。它支持两种运行模式:
- STDIO 模式:适用于基于命令行的应用程序和 AI 代理的标准输入/输出通信
- HTTP+SSE 模式:带有服务器发送事件的可网页访问的服务器,适用于浏览器和基于 HTTP 的客户端
该服务器提供了完整的任务管理操作、广泛的验证以及强大的错误处理功能。
特性
-
任务管理操作:
- 列出现有任务并具有过滤功能
- 创建具有自定义属性的新任务
- 更新任务详情(描述、状态、类别、优先级)
- 删除已完成或不再需要的任务
-
双接口模式:
- 支持命令行和 AI 代理集成的 STDIO 协议
- 带有网页界面的 HTTP+SSE 协议,适用于基于浏览器的访问
-
MCP 协议实现:
- 完整实现了 Model Context Protocol
- 任务数据结构资源
- 任务操作工具
- 错误处理及信息提示
-
质量保证:
- 用于验证的全面测试客户端
- 测试完成后自动关闭服务器
- 对 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 -
创建包含您的任务 API 凭证的
.env文件: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.js
HTTP 模式(用于网页访问)
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 服务器:
<!DOCTYPE html>
<html>
<head>
<title>Task Manager</title>
<script type="module">
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
});
</script>
</head>
<body>
<h1>Task Manager</h1>
<!-- Your interface elements here -->
</body>
</html>
可用工具
listTasks
列出所有可用任务。
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
}
});
createTask
创建新任务。
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
}
});
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
}
});
deleteTask
删除一个任务。
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许可证发布 - 详情请参阅LICENSE文件。
致谢
- 本项目使用@modelcontextprotocol/sdk实现MCP协议
- 为与AI工具和Web应用程序集成而构建