A bridge package that enables seamless integration between the Model Context Protocol (MCP) and AI SDK, allowing for efficient communication and tool execution between MCP servers and AI models.
mcp.config.json
npm install aisdk-mcp-bridge
mcp.config.json
file in your project root:{
"mcpServers": {
"twitter-mcp": {
"command": "npx",
"args": ["-y", "@enescinar/twitter-mcp"],
"env": {
"API_KEY": "your-twitter-api-key",
"API_SECRET_KEY": "your-twitter-api-secret",
"ACCESS_TOKEN": "your-twitter-access-token",
"ACCESS_TOKEN_SECRET": "your-twitter-access-token-secret"
}
},
"firecrawl": {
"command": "npx",
"args": ["-y", "mcp-server-firecrawl"],
"env": {
"FIRE_CRAWL_API_KEY": "your-firecrawl-api-key",
"FIRE_CRAWL_API_URL": "https://api.firecrawl.com"
}
}
}
}
import { generateText } from 'ai';
import { google } from '@ai-sdk/google';
import { getMcpTools, cleanupMcp, initializeMcp } from 'aisdk-mcp-bridge';
import dotenv from 'dotenv';
dotenv.config();
async function main() {
try {
// Initialize MCP
await initializeMcp({ debug: true });
// Get tools from all servers
const allTools = await getMcpTools({ debug: true });
// Or get tools from a specific server
const twitterTools = await getMcpTools({
debug: true,
serverName: 'twitter-mcp',
});
// Use tools with AI SDK
const result = await generateText({
model: google('gemini-1.5-pro'),
messages: [
{
role: 'system',
content:
'You are an AI assistant that uses various tools to help users.',
},
{
role: 'user',
content: 'Your task description here',
},
],
tools: twitterTools, // or allTools for all available tools
});
console.log('Result:', result.text);
} finally {
// Clean up resources
await cleanupMcp();
}
}
main().catch(error => {
console.error('Error:', error);
process.exit(1);
});
The mcp.config.json
file supports multiple servers and communication modes. Each server can be configured independently.
{
"mcpServers": {
"twitter-mcp": {
"command": "npx",
"args": ["-y", "@enescinar/twitter-mcp"],
"env": {
"API_KEY": "your-twitter-api-key",
"API_SECRET_KEY": "your-twitter-api-secret",
"ACCESS_TOKEN": "your-twitter-access-token",
"ACCESS_TOKEN_SECRET": "your-twitter-access-token-secret"
}
}
}
}
{
"mcpServers": {
"firecrawl": {
"command": "npx",
"args": ["-y", "mcp-server-firecrawl"],
"env": {
"FIRE_CRAWL_API_KEY": "your-firecrawl-api-key",
"FIRE_CRAWL_API_URL": "https://api.firecrawl.com"
}
}
}
}
{
"mcpServers": {
"sse-server": {
"command": "node",
"args": ["./server.js"],
"mode": "sse",
"sseOptions": {
"endpoint": "http://localhost:3000/events",
"headers": {},
"reconnectTimeout": 5000
}
}
}
}
The bridge supports different communication modes:
stdio Mode (Default)
SSE Mode (Server-Sent Events)
initializeMcp(options?: InitOptions): Promise<void>
Initialize the MCP service with the provided options.
interface InitOptions {
configPath?: string; // Path to mcp.config.json
debug?: boolean; // Enable debug logging
}
getMcpTools(options?: ToolOptions): Promise<ToolSet>
Get AI SDK-compatible tools from MCP servers.
interface ToolOptions {
debug?: boolean; // Enable debug logging
serverName?: string; // Optional server name to get tools from a specific server
}
executeMcpFunction(serverName: string, functionName: string, args: Record<string, unknown>): Promise<MCPToolResult>
Execute a specific function on an MCP server directly.
// Example
const result = await executeMcpFunction('twitter-mcp', 'postTweet', {
text: 'Hello from MCP!',
});
MCPConfig
(alias for MCPServersConfig
)Configuration type for MCP servers.
interface MCPConfig {
mcpServers: {
[key: string]: ServerConfig;
};
}
ServerConfig
Configuration for individual MCP servers.
interface ServerConfig {
command: string;
args?: string[];
env?: Record<string, string>;
mode?: 'stdio' | 'sse';
sseOptions?: {
endpoint: string;
headers?: Record<string, string>;
reconnectTimeout?: number;
};
}
MCPToolResult
Result type for MCP tool executions.
interface MCPToolResult {
success: boolean;
data?: unknown;
error?: string;
}
cleanupMcp(): Promise<void>
Clean up MCP resources and close all server connections.
The bridge includes comprehensive error handling for:
The bridge provides detailed logging through:
mcp-tools.log
: Server-side tool execution logsYou can enable detailed debug logging by setting the DEBUG environment variable:
# Enable all debug logs
DEBUG=* npm start
# Enable MCP debug logs
DEBUG=mcp npm start
# Enable all MCP namespace logs
DEBUG=mcp:* npm start
Debug logs will show:
The logging system supports three types of logs:
info
: General operational informationdebug
: Detailed debugging information (requires DEBUG env variable)error
: Error messages and stack traces (always logged)All logs are written to logs/mcp-tools.log
with the following format:
[TIMESTAMP] [TYPE] Message
{Optional JSON data}
npm install
Run the test suite:
npm test
Run specific tests:
npm run test:twitter
npm run test:firecrawl
We welcome contributions! Please see our Contributing Guide for details on:
Please note that this project is released with a Code of Conduct. By participating in this project you agree to abide by its terms.
For support:
See CHANGELOG.md for a list of changes and migration guides.
For security issues, please email ravi@caw.tech instead of using the public issue tracker.
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE file for details.
Seamless access to top MCP servers powering the future of AI integration.