chat-with-openai
Send a chat message to OpenAI and receive an AI-generated response. Use this tool for direct chat completions via the any-chat-completions-mcp server.
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:103-163 (handler)Handler for the chat tool. Connects to an OpenAI SDK compatible AI Integration. The tool name is dynamically generated as 'chat-with-{AI_CHAT_NAME_CLEAN}' where AI_CHAT_NAME_CLEAN is the lowercase, space-replaced AI_CHAT_NAME env var.
server.setRequestHandler(CallToolRequestSchema, async (request) => { switch (request.params.name) { 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 }; } } default: throw new Error("Unknown tool"); } }); - src/index.ts:80-94 (schema)Schema definition for the 'chat-with-{AI_CHAT_NAME_CLEAN}' tool. Input schema requires a 'content' string property.
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:78-97 (registration)Registration of the tool via ListToolsRequestSchema handler, exposing the dynamically-named chat tool.
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:103-163 (registration)Registration of the tool handler via CallToolRequestSchema, matching the dynamic tool name.
server.setRequestHandler(CallToolRequestSchema, async (request) => { switch (request.params.name) { 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 }; } } default: throw new Error("Unknown tool"); } }); - src/index.ts:22-41 (helper)Environment variable configuration that determines the tool name. AI_CHAT_NAME is lowercased and spaces replaced with hyphens to form the tool name 'chat-with-{AI_CHAT_NAME_CLEAN}'.
const AI_CHAT_NAME = process.env.AI_CHAT_NAME; const AI_CHAT_TIMEOUT = process.env.AI_CHAT_TIMEOUT || 30000; const AI_CHAT_SYSTEM_PROMPT = process.env.AI_CHAT_SYSTEM_PROMPT; if (!AI_CHAT_BASE_URL) { throw new Error("AI_CHAT_BASE_URL is required") } if (!AI_CHAT_KEY) { throw new Error("AI_CHAT_KEY is required") } if (!AI_CHAT_MODEL) { throw new Error("AI_CHAT_MODEL is required") } if (!AI_CHAT_NAME) { throw new Error("AI_CHAT_NAME is required") } const AI_CHAT_NAME_CLEAN = AI_CHAT_NAME.toLowerCase().replace(' ', '-')