/**
* MCP 工具注册
*/
import { Tool } from '@modelcontextprotocol/sdk/types.js';
// 项目管理工具
import { getProjects } from './getProjects';
import { getProjectById } from './getProjectById';
import { createProject } from './createProject';
// 任务管理工具
import { getTasks } from './getTasks';
import { createTask } from './createTask';
import { updateTaskStatus } from './updateTaskStatus';
// 批量操作工具
import { batchCreateTasks } from './batchCreateTasks';
// 禅道客户端和缓存
import { ZenTaoClient } from '../client';
import { LRUCache } from '../utils/cache';
// 创建工具数组
export function createTools(): Tool[] {
return [
// 项目管理工具
{
name: 'get_projects',
description: '获取禅道项目列表',
inputSchema: {
type: 'object',
properties: {
page: {
type: 'number',
description: '页码,默认为 1',
},
limit: {
type: 'number',
description: '每页数量,默认为 50',
},
status: {
type: 'string',
description: '项目状态过滤 (wait, doing, done, suspended, closed)',
},
orderBy: {
type: 'string',
description: '排序字段',
},
},
},
},
{
name: 'get_project_by_id',
description: '根据 ID 获取项目详情',
inputSchema: {
type: 'object',
properties: {
projectId: {
type: 'number',
description: '项目 ID',
},
},
required: ['projectId'],
},
},
{
name: 'create_project',
description: '创建新项目',
inputSchema: {
type: 'object',
properties: {
name: {
type: 'string',
description: '项目名称',
},
code: {
type: 'string',
description: '项目代码(唯一)',
},
begin: {
type: 'string',
description: '项目开始时间 (YYYY-MM-DD)',
},
end: {
type: 'string',
description: '项目结束时间 (YYYY-MM-DD)',
},
acl: {
type: 'string',
description: '访问控制 (private, open, extend)',
},
},
required: ['name', 'code', 'begin'],
},
},
// 任务管理工具
{
name: 'get_tasks',
description: '获取任务列表',
inputSchema: {
type: 'object',
properties: {
projectId: {
type: 'number',
description: '项目 ID 过滤',
},
productId: {
type: 'number',
description: '产品 ID 过滤',
},
storyId: {
type: 'number',
description: '需求 ID 过滤',
},
status: {
type: 'string',
description: '任务状态过滤 (wait, doing, done, blocked)',
},
assignedTo: {
type: 'string',
description: '负责人过滤',
},
pri: {
type: 'number',
description: '优先级过滤 (1-4, 1最高)',
},
page: {
type: 'number',
description: '页码,默认为 1',
},
limit: {
type: 'number',
description: '每页数量,默认为 50',
},
},
},
},
{
name: 'create_task',
description: '创建新任务',
inputSchema: {
type: 'object',
properties: {
name: {
type: 'string',
description: '任务名称',
},
project: {
type: 'number',
description: '所属项目 ID',
},
story: {
type: 'number',
description: '关联需求 ID',
},
assignedTo: {
type: 'string',
description: '负责人账号',
},
pri: {
type: 'number',
description: '优先级 (1-4, 1最高)',
},
estimate: {
type: 'number',
description: '预计工时 (小时)',
},
deadline: {
type: 'string',
description: '截止时间 (YYYY-MM-DD)',
},
},
required: ['name', 'project'],
},
},
{
name: 'update_task_status',
description: '更新任务状态',
inputSchema: {
type: 'object',
properties: {
taskId: {
type: 'number',
description: '任务 ID',
},
status: {
type: 'string',
description: '任务状态 (wait/doing/done/blocked/cancel/closed)',
},
},
required: ['taskId', 'status'],
},
},
// 批量操作工具
{
name: 'batch_create_tasks',
description: '批量创建任务',
inputSchema: {
type: 'object',
properties: {
items: {
type: 'array',
description: '任务列表',
items: {
type: 'object',
properties: {
name: {
type: 'string',
description: '任务名称',
},
project: {
type: 'number',
description: '项目 ID',
},
assignedTo: {
type: 'string',
description: '负责人账号',
},
pri: {
type: 'number',
description: '优先级',
},
estimate: {
type: 'number',
description: '预计工时',
},
},
required: ['name', 'project'],
},
},
options: {
type: 'object',
description: '批量操作选项',
properties: {
continueOnError: {
type: 'boolean',
description: '出错时是否继续,默认为 true',
},
reportMode: {
type: 'string',
enum: ['summary', 'detailed'],
description: '报告模式 (summary/detailed)',
},
maxConcurrency: {
type: 'number',
description: '最大并发数,默认为 10',
},
},
},
},
required: ['items'],
},
},
];
}
// 工具调用处理函数
export async function handleToolCall(
toolName: string,
args: any,
client: ZenTaoClient,
cache: LRUCache<string, any>
): Promise<any> {
switch (toolName) {
// 项目管理工具
case 'get_projects':
return await getProjects(client, cache, args);
case 'get_project_by_id':
return await getProjectById(client, cache, args.projectId);
case 'create_project':
return await createProject(client, cache, args);
// 任务管理工具
case 'get_tasks':
return await getTasks(client, cache, args);
case 'create_task':
return await createTask(client, cache, args);
case 'update_task_status':
return await updateTaskStatus(client, cache, args.taskId, args.status);
// 批量操作工具
case 'batch_create_tasks':
return await batchCreateTasks(client, cache, args.items, args.options);
default:
return {
content: [
{
type: 'text',
text: `未知的工具: ${toolName}`,
},
],
isError: true,
};
}
}