MCP: instructions для Claude (что у нас на сегодня -> очередь бухгалтерии) и промпт today; текст в p-mcp-instructions.md
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -466,10 +466,20 @@ export const TOOLS: { name: string; description: string; inputSchema: Json; anno
|
||||
},
|
||||
];
|
||||
|
||||
/** Встроенный текст инструкций (копия: docs/.../p-mcp-instructions.md; сервис подставляет файл, если он доступен). */
|
||||
export const DEFAULT_INSTRUCTIONS =
|
||||
"Коннектор очереди ручного разбора бухгалтерии KH Build. На вопросы вроде «что у нас на сегодня», «что по делам», «что горит» кроме календаря и других источников обязательно вызови acc_review_list и покажи таблицу очереди как есть; затем дай общую сводку. Действия над несколькими задачами - одним вызовом acc_review_batch. acc_review_done - только после подтверждения пользователя и проводки в QuickBooks.";
|
||||
|
||||
/** Блок «## Инструкции» из файла p-mcp-instructions.md. */
|
||||
export function instructionsFromFile(text: string): string {
|
||||
const m = text.split(/\n## Инструкции\s*\n/)[1];
|
||||
return (m ?? text).trim();
|
||||
}
|
||||
|
||||
const rpcError = (id: unknown, code: number, message: string): Json => ({ jsonrpc: "2.0", id: id ?? null, error: { code, message } });
|
||||
|
||||
/** Обрабатывает одно JSON-RPC сообщение от аутентифицированного человека; null - уведомление (ответа нет). */
|
||||
export async function handleMcpMessage(tools: ReviewTools, personId: string, msg: Json): Promise<Json | null> {
|
||||
export async function handleMcpMessage(tools: ReviewTools, personId: string, msg: Json, instructions: string = DEFAULT_INSTRUCTIONS): Promise<Json | null> {
|
||||
const id = msg?.id;
|
||||
const method = msg?.method;
|
||||
if (typeof method !== "string") return rpcError(id, -32600, "invalid request");
|
||||
@@ -477,10 +487,24 @@ export async function handleMcpMessage(tools: ReviewTools, personId: string, msg
|
||||
switch (method) {
|
||||
case "initialize": {
|
||||
const asked = (msg.params as Json | undefined)?.protocolVersion;
|
||||
return { jsonrpc: "2.0", id, result: { protocolVersion: typeof asked === "string" ? asked : "2025-03-26", capabilities: { tools: {}, resources: {} }, serverInfo: { name: "accounting_automation", version: "0.1.0" } } };
|
||||
return { jsonrpc: "2.0", id, result: { protocolVersion: typeof asked === "string" ? asked : "2025-03-26", capabilities: { tools: {}, resources: {}, prompts: {} }, serverInfo: { name: "accounting_automation", version: "0.1.0" }, instructions } };
|
||||
}
|
||||
case "ping":
|
||||
return { jsonrpc: "2.0", id, result: {} };
|
||||
case "prompts/list":
|
||||
return { jsonrpc: "2.0", id, result: { prompts: [{ name: "today", description: "Что у нас на сегодня: календарь и очередь разбора бухгалтерии" }] } };
|
||||
case "prompts/get": {
|
||||
const name = (msg.params as Json | undefined)?.name;
|
||||
if (name !== "today") return rpcError(id, -32602, `неизвестный промпт ${String(name)}`);
|
||||
return {
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {
|
||||
description: "Что у нас на сегодня",
|
||||
messages: [{ role: "user", content: { type: "text", text: "Что у нас на сегодня? Посмотри календарь и очередь разбора бухгалтерии (acc_review_list), покажи таблицу очереди и дай общую сводку." } }],
|
||||
},
|
||||
};
|
||||
}
|
||||
case "resources/list":
|
||||
return { jsonrpc: "2.0", id, result: { resources: [{ uri: REVIEW_UI_URI, name: "Таблица очереди разбора", mimeType: REVIEW_UI_MIME }] } };
|
||||
case "resources/read": {
|
||||
@@ -516,6 +540,8 @@ export async function handleMcpMessage(tools: ReviewTools, personId: string, msg
|
||||
}
|
||||
|
||||
export interface McpDeps extends ReviewDeps {
|
||||
/** Текст инструкций для Claude (поле instructions при initialize). */
|
||||
instructions?: string;
|
||||
settings: Pick<AccountingSettings, "reviewers" | "mcp">;
|
||||
/** Проверка JWT Keycloak (для тестов - с локальным JWKS). */
|
||||
verifyJwt?: JwtVerifier;
|
||||
@@ -543,11 +569,11 @@ export function registerMcp(app: FastifyInstance, deps: McpDeps): ReviewTools {
|
||||
}
|
||||
const body = req.body as unknown;
|
||||
if (Array.isArray(body)) {
|
||||
const out = (await Promise.all(body.map((m) => handleMcpMessage(tools, personId, m as Json)))).filter((x): x is Json => x !== null);
|
||||
const out = (await Promise.all(body.map((m) => handleMcpMessage(tools, personId, m as Json, deps.instructions)))).filter((x): x is Json => x !== null);
|
||||
return out.length ? reply.send(out) : reply.code(202).send();
|
||||
}
|
||||
if (!body || typeof body !== "object") return reply.code(400).send(rpcError(null, -32700, "parse error"));
|
||||
const res = await handleMcpMessage(tools, personId, body as Json);
|
||||
const res = await handleMcpMessage(tools, personId, body as Json, deps.instructions);
|
||||
return res ? reply.send(res) : reply.code(202).send();
|
||||
});
|
||||
app.get("/mcp", async (_req, reply) => reply.code(405).header("Allow", "POST").send({ error: "используйте POST" }));
|
||||
|
||||
Reference in New Issue
Block a user