/ easyshell-mcp / src / tools / ai-chat.ts
ai-chat.ts
 1  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
 2  import { z } from "zod";
 3  import type { EasyShellClient } from "../client.js";
 4  import { toMcpError } from "../errors.js";
 5  
 6  type AiChatParams = {
 7    message: string;
 8    sessionId?: string;
 9    targetAgentIds?: string[];
10    skipPlanning?: boolean;
11  };
12  
13  export function registerAiChatTools(server: McpServer, client: EasyShellClient) {
14    server.tool(
15      "ai_chat",
16      "Send a natural language message to EasyShell's AI engine for task orchestration. The AI can analyze your intent, create execution plans, dispatch scripts to hosts, and return structured analysis. This is the most powerful tool — use it for complex multi-host operations described in natural language.",
17      {
18        message: z
19          .string()
20          .describe(
21            "Natural language instruction, e.g. 'Check disk usage on all production hosts'"
22          ),
23        sessionId: z.string().optional().describe("Session ID for conversation continuity"),
24        targetAgentIds: z.array(z.string()).optional().describe("Limit execution to specific hosts"),
25        skipPlanning: z
26          .boolean()
27          .default(false)
28          .optional()
29          .describe("Skip the planning phase for simple commands"),
30      },
31      async ({ message, sessionId, targetAgentIds, skipPlanning }: AiChatParams) => {
32        try {
33          const result = await client.post("/api/v1/ai/chat", {
34            message,
35            sessionId,
36            targetAgentIds,
37            skipPlanning,
38            enableTools: true,
39          });
40  
41          return {
42            content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }],
43          };
44        } catch (error) {
45          return toMcpError(error);
46        }
47      }
48    );
49  }