> ## Documentation Index
> Fetch the complete documentation index at: https://docs.praison.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# MCP Tools

> Model Context Protocol integration for AI agents

# MCP Tools

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

## Quick Start

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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)

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

### SSE (Server-Sent Events)

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

### HTTP

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

### WebSocket

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
const mcp = await createMCP({
  transport: {
    type: 'websocket',
    url: 'wss://mcp-server.example.com/ws',
  },
});
```

## OAuth Authentication

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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:

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
// 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:

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
// 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);
```

## Popular MCP Servers

| Server       | Package                                     | Description       |
| ------------ | ------------------------------------------- | ----------------- |
| Filesystem   | `@modelcontextprotocol/server-filesystem`   | File operations   |
| GitHub       | `@modelcontextprotocol/server-github`       | GitHub API        |
| Slack        | `@modelcontextprotocol/server-slack`        | Slack integration |
| PostgreSQL   | `@modelcontextprotocol/server-postgres`     | Database queries  |
| Brave Search | `@modelcontextprotocol/server-brave-search` | Web search        |

## Example: Filesystem Server

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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:

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { closeMCPClient, closeAllMCPClients } from 'praisonai-ts';

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

// Close all clients
await closeAllMCPClients();
```

## Environment Variables

| Variable         | Required | Description             |
| ---------------- | -------- | ----------------------- |
| `OPENAI_API_KEY` | Yes      | For the agent           |
| Server-specific  | Varies   | MCP 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

## Related

* [Tools](/docs/js/tools) - Tool system overview
* [Agent](/docs/js/agent) - Agent configuration
