UNPKG

ask-ai-mcp

Version:

A Model Context Protocol server enabling AI-to-AI collaboration across multiple providers.

99 lines (93 loc) 4.42 kB
#!/usr/bin/env node import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; import { AIService } from "./ai-service.js"; import { getConfig, SERVER_INFO } from "./config.js"; async function createServer() { const config = getConfig(); const aiService = new AIService(config); const server = new McpServer({ name: SERVER_INFO.name, version: SERVER_INFO.version, }); server.tool("ask_ai", `Call another AI model for help, a fresh perspective, or brainstorming across technical, creative, research, or strategic topics. When should you call me? - You're stuck on a problem or need help debugging or researching. - You seek alternative viewpoints on design, planning, or creative work. - You want a review or suggestions for improvement (code, writing, strategy, etc.). - You need new ideas, inspiration, or broader perspectives. How to use me for the best results: - Put your focused question in the 'question' field. - Provide comprehensive background in the 'context' field (code, data, requirements, conversation history, constraints, etc.) - the more detailed information you provide, the more accurate and reliable the response will be. > Warning: Vague questions with minimal context will likely result in imprecise or unreliable answers. Quality and completeness of your input directly impacts the quality of AI assistance. How to phrase your question effectively: - Prefer open-ended questions that explain the issue you face and the goal you aim to achieve. - Avoid adding unverified assumptions or early conclusions; they might be wrong and lead both of us astray. **CRITICAL WARNING:** The response comes from another AI. It could be inaccurate or misleading. You MUST verify any information, code, or suggestions provided. Treat the answer as guidance to be checked, not as fact.`, { question: z .string() .describe("The specific question or query to ask the AI model"), context: z .string() .optional() .describe("Optional context: background information, code snippets, previous discussion results, conversation history, and any other relevant details"), }, async ({ question, context }) => { try { const request = { question }; if (context !== undefined) { request.context = context; } const response = await aiService.askAI(request); return { content: [ { type: "text", text: `Response from "${response.provider}/${response.model})": IMPORTANT: The following content is generated by AI. Due to technical limitations of LLM including hallucinations, knowledge constraints, and potential inaccuracies due to insufficient context details, you MUST read this response with caution. Assume there could be fundamental factual errors. You MUST carefully evaluate and verify before adoption. Do not accept any statements as facts without critical thinking, as this may lead to serious misleading. \n\n${response.response} `, }, ], isError: false, }; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown error occurred"; return { content: [ { type: "text", text: `**Error**: Failed to get AI response: ${errorMessage}\n\nPlease ask user to configuration and try again.`, }, ], isError: true, }; } }); return server; } async function main() { try { const server = await createServer(); const transport = new StdioServerTransport(); await server.connect(transport); console.error("Ask AI MCP Server is running..."); } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown error occurred"; console.error(`❌ Failed to start server: ${errorMessage}`); process.exit(1); } } main().catch((error) => { console.error("Fatal error:", error); process.exit(1); }); //# sourceMappingURL=server.js.map