mcp-tools
Installation
SKILL.md
MCP Tools 開發指南
本專案的 MCP Tools 位於 pai-bot/src/mcp/tools/。
架構模式
Service Layer (ts-results) MCP Tool Layer
┌─────────────────────┐ ┌──────────────────────┐
│ Result<T, Error> │ --> │ { content, isError } │
└─────────────────────┘ └──────────────────────┘
Service Layer
使用 ts-results 回傳 Result<T, Error>:
import { Err, Ok, type Result } from "ts-results";
export async function getData(): Promise<Result<Data, Error>> {
try {
const data = await api.fetch();
return Ok(data);
} catch (error) {
return Err(error instanceof Error ? error : new Error(String(error)));
}
}
MCP Tool Layer
1. 註冊工具
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
export function registerMyTools(server: McpServer): void {
server.registerTool(
"tool_name", // 命名:category_action
{
title: "Tool Title",
description: "工具描述(中文)",
inputSchema: {
param: z.string().describe("參數說明"),
optional: z.number().optional().describe("可選參數"),
},
},
async ({ param, optional = 5 }) => {
// 實作
}
);
}
2. 處理 Result
async ({ query }) => {
const result = await service.getData(query);
if (result.err) {
return {
content: [{ type: "text", text: `錯誤: ${result.val.message}` }],
isError: true,
};
}
return {
content: [{ type: "text", text: JSON.stringify(result.val, null, 2) }],
};
}
3. 無 Result 的 Service(使用 try-catch)
async ({ query }) => {
try {
const data = await legacyService.fetch(query);
return {
content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
};
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
return {
content: [{ type: "text", text: `錯誤: ${message}` }],
isError: true,
};
}
}
命名規範
| 類別 | 格式 | 範例 |
|---|---|---|
| 工具名稱 | category_action |
google_calendar_list, memory_search |
| 函數名稱 | registerXxxTools |
registerGoogleTools |
錯誤訊息格式
`${Category} 錯誤: ${error.message}`
// 範例: "Google Calendar 錯誤: Invalid credentials"
註冊入口
在 pai-bot/src/mcp/index.ts 註冊:
import { registerMyTools } from "./tools/my-tools";
export function createMcpServer(): McpServer {
const server = new McpServer({ name: "pai-bot", version: "1.0.0" });
registerMyTools(server);
return server;
}
Checklist
- Service 使用
Result<T, Error> - Tool 處理
result.err並回傳isError: true - 使用 Zod schema 定義參數
- 錯誤訊息包含分類前綴
- 在
index.ts註冊
Related skills
More from wayne930242/merlin-my-pai
life
生活管理系統。觸發:早安, 晚安, 今天, 規劃, 回顧, 健康, 投資, 股票, 追蹤, watchlist, morning, evening, daily, plan, review
1coding
代碼撰寫與自動化。觸發:code, script, tool, automate, batch, crawler, API, 寫程式, 腳本, 自動化
1research
研究與資訊蒐集。觸發:research, investigate, compare, evaluate, analyze, explore, 研究, 比較, 分析, 調查
1learning
學習輔助與知識管理。觸發:learn, note, knowledge, memory, review, understand, teach me, 學習, 筆記, 教我, 解釋
1google
Google services integration. Use when user mentions calendar, schedule, meeting, drive, files, gmail, email, contacts, tasks, todo, task list.
1memory
長期記憶管理。主動保存重要資訊、搜尋相關記憶、整理記憶。觸發:remember, 記住, 記憶, memory, 之前說過, 上次, 忘記
1