import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { Server } from "@modelcontextprotocol/sdk/server/index.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js"; import { runCli } from "./cli/runner.js"; import { classifyCliError } from "./cli/errors.js"; import { VERSION } from "./version.js"; const serverEntryFile = fileURLToPath(import.meta.url); const pluginRoot = serverEntryFile.includes("/dist/mcp/") || serverEntryFile.includes("\\dist\\mcp\\") ? dirname(dirname(dirname(serverEntryFile))) : dirname(dirname(serverEntryFile)); export const SEAL_CLI_TOOL_NAME = "seal_cli"; export const SEAL_MCP_ENABLED_ENV = "SEAL_USE_MCP_ENABLED"; export const LEGACY_SEAL_MCP_ENABLED_ENV = "SEAL_HOME_MCP_ENABLED"; export function resolveMcpEnabled( value = process.env[SEAL_MCP_ENABLED_ENV] ?? process.env[LEGACY_SEAL_MCP_ENABLED_ENV] ): boolean { if (value === undefined) return false; return ["1", "true", "yes", "on"].includes(value.trim().toLowerCase()); } export function createServer(options: { enableCli?: boolean } = {}) { const enableCli = options.enableCli ?? true; const server = new Server( { name: "seal-use", version: VERSION }, { capabilities: { tools: {} } } ); server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: enableCli ? [ { name: SEAL_CLI_TOOL_NAME, description: "Run seal-use CLI commands. See the seal-use skill for command reference.", inputSchema: { type: "object", properties: { command: { type: "string", description: "A seal-use CLI command, with or without the leading 'seal-use' or 'seal-home'." } }, required: ["command"], additionalProperties: false } } ] : [] })); if (enableCli) server.setRequestHandler(CallToolRequestSchema, async (request) => { const args = request.params.arguments; const command = args && typeof args.command === "string" ? args.command : undefined; if (!command?.trim()) { return cliResult({ stdout: "", stderr: "seal_cli requires a non-empty command string", isError: true, exitCode: 2 }); } try { const tokens = tokenizeCommand(command); const cliArgs = normalizeCliArgs(tokens); if (!cliArgs.length) { throw new Error("seal_cli requires a non-empty command string"); } return cliResult(await runCli(cliArgs, { appRoot: pluginRoot, entryFile: serverEntryFile, launcher: { command: process.execPath, args: [serverEntryFile, "--seal-home-cli"] } })); } catch (error) { const classification = classifyCliError(error); return cliResult({ stdout: "", stderr: error instanceof Error ? error.message : String(error), isError: true, exitCode: classification.exitCode }); } }); return server; } export async function startServer() { const enableCli = resolveMcpEnabled(); const server = createServer({ enableCli }); await server.connect(new StdioServerTransport()); console.error( enableCli ? "seal-use MCP server running on stdio" : "seal-use MCP server connected with seal_cli disabled by configuration" ); } export function tokenizeCommand(command: string): string[] { const tokens: string[] = []; let token = ""; let quote: "'" | '"' | undefined; let escaped = false; let tokenStarted = false; for (const character of command) { if (escaped) { token += character; escaped = false; tokenStarted = true; continue; } if (character === "\\" && quote !== "'") { escaped = true; tokenStarted = true; continue; } if (quote) { if (character === quote) { quote = undefined; } else { token += character; } tokenStarted = true; continue; } if (character === "'" || character === '"') { quote = character; tokenStarted = true; continue; } if (/\s/.test(character)) { if (tokenStarted) { tokens.push(token); token = ""; tokenStarted = false; } continue; } token += character; tokenStarted = true; } if (escaped) throw new Error("Invalid command: trailing escape"); if (quote) throw new Error("Invalid command: unterminated quote"); if (tokenStarted) tokens.push(token); return tokens; } function normalizeCliArgs(tokens: string[]): string[] { const first = tokens[0]; if (!first) return []; if ( first === "seal-use" || first === "seal-home" || first.endsWith("/seal-use") || first.endsWith("/seal-home") ) { return tokens.slice(1); } return tokens; } function cliResult(result: { stdout: string; stderr: string; isError: boolean; exitCode: number }) { const text = result.isError ? result.stderr || result.stdout : result.stdout; return { content: [ { type: "text" as const, text } ], isError: result.isError }; } async function runEmbeddedCli() { const result = await runCli(process.argv.slice(3), { appRoot: pluginRoot, entryFile: serverEntryFile, launcher: { command: process.execPath, args: [serverEntryFile, "--seal-use-cli"] } }); if (result.stdout) process.stdout.write(result.stdout); if (result.stderr) process.stderr.write(`${result.stderr}${result.stderr.endsWith("\n") ? "" : "\n"}`); process.exitCode = result.exitCode; } const isMainModule = Boolean( (import.meta as any).main || (!import.meta.url.includes("$bunfs") && process.argv[1] && resolve(process.argv[1]) === resolve(serverEntryFile)) ); if (isMainModule) { if (process.argv[2] === "--seal-use-cli" || process.argv[2] === "--seal-home-cli") { await runEmbeddedCli(); } else { startServer().catch((error) => { console.error(error); process.exitCode = 1; }); } }