Skip to main content

MCP Tools

Integrate Model Context Protocol (MCP) servers to extend agent capabilities with external tools, resources, and prompts.

Quick Start

import { Agent, createMCP } from 'praisonai-ts';

// Connect to MCP server
const mcp = await createMCP({
  transport: {
    type: 'stdio',
    command: 'npx',
    args: ['-y', '@modelcontextprotocol/server-filesystem', '/path/to/dir'],
  },
});

// Get tools from MCP server
const tools = await mcp.tools();

// Create agent with MCP tools
const agent = new Agent({
  name: 'FileAgent',
  instructions: 'You help manage files.',
  tools: Object.values(tools),
});

const response = await agent.chat('List all files in the directory');

Transport Types

Stdio (Local Servers)

const mcp = await createMCP({
  transport: {
    type: 'stdio',
    command: 'npx',
    args: ['-y', '@modelcontextprotocol/server-filesystem', '/path'],
    env: { NODE_ENV: 'production' },
  },
});

SSE (Server-Sent Events)

const mcp = await createMCP({
  transport: {
    type: 'sse',
    url: 'https://mcp-server.example.com/sse',
    headers: { Authorization: 'Bearer token' },
  },
});

HTTP

const mcp = await createMCP({
  transport: {
    type: 'http',
    url: 'https://mcp-server.example.com/api',
    headers: { 'X-API-Key': 'key' },
  },
});

WebSocket

const mcp = await createMCP({
  transport: {
    type: 'websocket',
    url: 'wss://mcp-server.example.com/ws',
  },
});

OAuth Authentication

import { createMCP, type OAuthClientProvider } from 'praisonai-ts';

const oauthProvider: OAuthClientProvider = {
  getAccessToken: async () => {
    return await getStoredToken();
  },
  refreshToken: async () => {
    return await refreshOAuthToken();
  },
};

const mcp = await createMCP({
  transport: {
    type: 'http',
    url: 'https://mcp-server.example.com/api',
    authProvider: oauthProvider,
  },
});

Resources

Access resources from MCP servers:
// List available resources
const resources = await mcp.listResources();
console.log('Resources:', resources);

// Read a resource
const content = await mcp.readResource('file:///path/to/file.txt');
console.log('Content:', content.text);

Prompts

Use prompts from MCP servers:
// List available prompts
const prompts = await mcp.listPrompts();
console.log('Prompts:', prompts);

// Get a prompt
const prompt = await mcp.getPrompt('summarize', { length: 'short' });
console.log('Prompt messages:', prompt.messages);
ServerPackageDescription
Filesystem@modelcontextprotocol/server-filesystemFile operations
GitHub@modelcontextprotocol/server-githubGitHub API
Slack@modelcontextprotocol/server-slackSlack integration
PostgreSQL@modelcontextprotocol/server-postgresDatabase queries
Brave Search@modelcontextprotocol/server-brave-searchWeb search

Example: Filesystem Server

import { Agent, createMCP } from 'praisonai-ts';

const mcp = await createMCP({
  transport: {
    type: 'stdio',
    command: 'npx',
    args: ['-y', '@modelcontextprotocol/server-filesystem', './documents'],
  },
});

const tools = await mcp.tools();

const agent = new Agent({
  name: 'FileManager',
  instructions: 'You help manage files and directories.',
  tools: Object.values(tools),
});

await agent.chat('Create a new file called notes.txt with "Hello World"');

Example: GitHub Server

const mcp = await createMCP({
  transport: {
    type: 'stdio',
    command: 'npx',
    args: ['-y', '@modelcontextprotocol/server-github'],
    env: { GITHUB_TOKEN: process.env.GITHUB_TOKEN },
  },
});

const tools = await mcp.tools();

const agent = new Agent({
  name: 'GitHubAgent',
  instructions: 'You help with GitHub operations.',
  tools: Object.values(tools),
});

await agent.chat('List my recent repositories');

Cleanup

Always close MCP connections when done:
import { closeMCPClient, closeAllMCPClients } from 'praisonai-ts';

// Close specific client
await closeMCPClient('my-client-id');

// Close all clients
await closeAllMCPClients();

Environment Variables

VariableRequiredDescription
OPENAI_API_KEYYesFor the agent
Server-specificVariesMCP server requirements

Best Practices

  1. Close connections - Always close MCP clients when done
  2. Handle errors - MCP servers may fail or timeout
  3. Use appropriate transport - Stdio for local, HTTP/SSE for remote
  4. Secure credentials - Use OAuth for authenticated servers
  • Tools - Tool system overview
  • Agent - Agent configuration