Package Exports
- openai-plugins
- openai-plugins/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (openai-plugins) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
openai-mcp
A pluggable middleware extension for the OpenAI SDK with support for Model Context Protocol (MCP) and multi-provider model routing.
Features
- Multi-provider model routing: Seamlessly use models from OpenAI, Anthropic, Gemini, and others through a unified interface
- MCP dynamic tool orchestration: Connect to Model Context Protocol servers to enable advanced tool usage
- Two-pass completions: Get more accurate and reliable model responses
- Pluggable middleware: Easily extend and customize the behavior
Installation
npm install openai-mcp
Quick Start
import OpenAI from 'openai-mcp';
// Create a client with your API key
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY, // Change to 'ANTHROPIC_API_KEY' or 'GEMINI_API_KEY' to use other providers
// Enable plugins
plugins: ['multiModel', 'mcp'], // multiModel is on by default
// MCP configuration (optional)
mcp: {
serverUrl: 'http://localhost:3000/mcp',
systemPrompt: 'You are a helpful assistant with tools.'
}
});
// Use like the standard OpenAI SDK
async function main() {
const response = await client.chat.completions.create({
model: 'gpt-4o', // or 'claude-3-opus-20240229' or 'gemini-1.5-pro' to use other providers
messages: [
{ role: 'user', content: 'Hello, can you help me?' }
]
});
console.log(response.choices[0].message.content);
}
main().catch(console.error);
API Keys
You can provide API keys in two ways:
Direct in the constructor (recommended):
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY // Used for all providers });
Environment variables (if no apiKey provided):
OPENAI_API_KEY=sk-... # For OpenAI models ANTHROPIC_API_KEY=sk-ant-... # For Anthropic models GEMINI_API_KEY=... # For Gemini models
The library will use API keys in this priority:
- The apiKey provided in the constructor (for all providers)
- Provider-specific environment variables as fallback
MCP Tool Usage
When connected to a Model Context Protocol server, the library automatically discovers and makes available all tools registered with the server:
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
plugins: ['mcp'],
mcp: {
serverUrl: 'http://localhost:3000/mcp',
maxToolCalls: 10, // Maximum number of tool calls per request
toolTimeoutSec: 60, // Timeout for each tool call
disconnectAfterUse: true // Disconnect after each request
}
});
// Tools will be automatically discovered and used
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'user', content: 'What is the weather like in New York?' }
]
});
Custom Plugins
You can create and register your own plugins:
const myPlugin = {
name: 'myPlugin',
async handle(params, next) {
console.log('Request params:', params);
// Modify params if needed
const result = await next(params);
console.log('Response:', result);
return result;
}
};
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
plugins: ['multiModel'],
customPlugins: [myPlugin]
});
License
MIT