This library allows you connect your OpenAI models to mcp.run and expose your installed servlets as tools which can be invoked in process (without spinning up many server processes).
It accomplishes this by bundling an MCP client, server, and our wasm technology.
You just need the mcpx-openai library and the openai library (if you don't already have it).
npm install @dylibso/mcpx-openai openai --save
To get an mcp.run session id, run this command and follow the instructions:
npx --yes -p @dylibso/mcpx@latest gen-session
McpxOpenAI presents as a wrapper around the OpenAI Node library.
import OpenAI from "openai";
import McpxOpenAI from "@dylibso/mcpx-openai"
async function main() {
// Create your OpenAI client as normal
const openai = new OpenAI({
apiKey: String(process.env['OPENAI_API_KEY']),
})
const sessionId = String(process.env['MCP_RUN_SESSION_ID'])
// Wrap with McpxOpenAI
const mcpx = await McpxOpenAI.create({
openai,
sessionId,
})
// NOTE: consider writing a system message to guide the agent into
// getting the behavior you want for more complex scenarios
const messages = [];
// call any tool compatible api, e.g chat completion:
// let's ask it to evalute some javascript. If you have
// this tool installed: https://www.mcp.run/bhelx/eval-js it should
// determine and use this to evaluate it in a sandbox
messages.push({
role: 'user',
content: `
Write a djb2hash function in javascript and evalute it on the string "Hello, World!"
`
})
// this will automatically process all tool calls
// until there are none left
let response = await mcpx.chatCompletionCreate({
model: 'gpt-4o',
temperature: 0,
messages,
});
console.log(response.choices[0]?.message)
//=> The DJB2 hash of the string "Hello, World!" is `-1763540338`.
}
main()