MCP ExplorerExplorer

Mcpx Openai Node

@dylibsoon 9 months ago
15 BSD-3-Clause
FreeCommunity
AI Systems
OpenAI node support for MCPX

Overview

What is Mcpx Openai Node

mcpx-openai-node is a library that connects OpenAI models to MCPX, allowing users to expose installed servlets as tools that can be invoked in-process without the need for multiple server processes.

Use cases

Use cases include evaluating JavaScript code, building chatbots, and creating interactive AI applications that leverage OpenAI’s models within a single process.

How to use

To use mcpx-openai-node, install the library along with the OpenAI library via npm. Then, generate an MCP.run session ID and wrap your OpenAI client with mcpx-openai-node to start making API calls.

Key features

Key features include seamless integration with OpenAI models, the ability to expose servlets as tools, and the use of MCP technology for efficient process management.

Where to use

mcpx-openai-node can be used in various fields such as software development, AI model deployment, and any application that requires efficient interaction with OpenAI’s capabilities.

Content

MCPX OpenAI Client

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.

Usage

Install

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

Code

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()

Examples

Tools

No tools

Comments

Recommend MCP Servers

View All MCP Servers