chat-with-openai
Interact with OpenAI's chat completions to process and generate text responses using the Any Chat Completions MCP server for streamlined integration.
Instructions
Text chat with OpenAI
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | The content of the chat to send to OpenAI |
Implementation Reference
- src/index.ts:105-158 (handler)Executes the 'chat-with-openai' tool by creating an OpenAI client with configured env vars, sending a chat completion request with optional system prompt, and returning the AI response or error.
case `chat-with-${AI_CHAT_NAME_CLEAN}`: { const content = String(request.params.arguments?.content) if (!content) { throw new Error("Content is required") } const client = new OpenAI({ apiKey: AI_CHAT_KEY, baseURL: AI_CHAT_BASE_URL, timeout: parseInt(`${AI_CHAT_TIMEOUT}`, 10), }); try { const messages: [OpenAI.ChatCompletionMessageParam] = [ { role: 'user', content: content } ]; if (AI_CHAT_SYSTEM_PROMPT) { messages.unshift({ role: 'system', content: `${AI_CHAT_SYSTEM_PROMPT}` }); } messages.push(); const chatCompletion = await client.chat.completions.create({ messages, model: AI_CHAT_MODEL.trim(), // Trim to remove any whitespace }); const responseContent = chatCompletion.choices[0]?.message?.content; if (!responseContent) { throw new Error('No response content received from API'); } return { content: [ { type: "text", text: responseContent } ] }; } catch (error: any) { const errorMessage = error.response?.data?.error?.message || error.message || 'Unknown error occurred'; console.error('Chat completion error:', errorMessage); return { content: [ { type: "text", text: `Error: ${errorMessage}` } ], isError: true }; } } - src/index.ts:78-97 (registration)Registers the 'chat-with-openai' tool (dynamically named via AI_CHAT_NAME_CLEAN) in the list of available tools, including its description and input schema.
server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: `chat-with-${AI_CHAT_NAME_CLEAN}`, description: `Text chat with ${AI_CHAT_NAME}`, inputSchema: { type: "object", properties: { content: { type: "string", description: `The content of the chat to send to ${AI_CHAT_NAME}`, } }, required: ["content"] } } ] }; }); - src/index.ts:84-93 (schema)Defines the input schema for the 'chat-with-openai' tool, requiring a 'content' string.
inputSchema: { type: "object", properties: { content: { type: "string", description: `The content of the chat to send to ${AI_CHAT_NAME}`, } }, required: ["content"] } - src/index.ts:41-41 (helper)Helper that computes the clean lowercase hyphenated name for the chat tool, e.g., 'openai' from 'OpenAI', used to form 'chat-with-openai'.
const AI_CHAT_NAME_CLEAN = AI_CHAT_NAME.toLowerCase().replace(' ', '-')