Skip to main content
Connect agents to Model Context Protocol (MCP) servers to give them access to external tools, data, and services.

Quick Start

1

Simple Usage

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

const mcp = new MCP('http://127.0.0.1:8080/sse');
await mcp.initialize();
2

With Configuration

const toolFunctions = Object.fromEntries(
  [...mcp].map(tool => [tool.name, async (args: any) => tool.execute(args)])
);

const agent = new Agent({
  instructions: 'You are a helpful assistant with access to MCP tools.',
  name: 'MCPAgent',
  tools: mcp.toOpenAITools(),
  toolFunctions,
});

const response = await agent.runSync('What tools are available?');
console.log(response);

await mcp.close();

Transport Selection

The MCP class picks the right transport automatically — no config objects needed.
URL patterntransport paramTransport used
http://host/sse'auto' (default)SSE
http://host/api'auto' (default)HTTP-Streaming
any URL'sse'SSE (forced)
any URL'http-streaming'HTTP-Streaming (forced)
Auto-detection is suffix-based: only URLs that end exactly with /sse select SSE. A URL like https://api.example.com/sse/v2 will not auto-select SSE — pass 'sse' explicitly.

Examples

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

async function main() {
  // URL ends with /sse → auto-selects SSE
  const mcp = new MCP('http://127.0.0.1:8080/sse');
  await mcp.initialize();
  console.log(`Transport: ${mcp.transportType}`); // 'sse'

  const toolFunctions = Object.fromEntries(
    [...mcp].map(tool => [tool.name, async (args: any) => tool.execute(args)])
  );

  const agent = new Agent({
    instructions: 'You are a helpful assistant.',
    name: 'AutoMCPAgent',
    tools: mcp.toOpenAITools(),
    toolFunctions,
  });

  const response = await agent.runSync('What tools are available?');
  console.log(response);

  await mcp.close();
}

main().catch(console.error);

Configuration Options

OptionTypeDefaultDescription
urlstringrequiredMCP server URL
transportTransportType'auto'One of 'auto' | 'sse' | 'http-streaming'
debugbooleanfalseLog transport selection and tool count
import { MCP, TransportType } from 'praisonai-ts';

const mcp = new MCP(
  'http://127.0.0.1:8080/stream',  // url
  'http-streaming',                  // transport
  true                               // debug
);

Instance API

Method / PropertyReturnsDescription
initialize()Promise<void>Connect and load tools from the server
close()Promise<void>Disconnect and release resources
toOpenAITools()OpenAITool[]Convert tools to OpenAI function-call format
[...mcp]MCPTool[]Iterate over available tools
mcp.toolsMCPTool[]Array of loaded tools (after initialize())
mcp.isConnectedbooleantrue after initialize(), false after close()
mcp.transportTypestring'sse', 'http-streaming', or 'not initialized'

New Exports

import { 
  MCP,                    // Unified MCP client (use this)
  TransportType,          // 'auto' | 'sse' | 'http-streaming'
  HTTPStreamingTransport, // Low-level transport (advanced use)
  MCPHttpStreaming,       // Standalone HTTP-Streaming client
  MCPTool,               // Tool type
  MCPToolInfo            // Tool info type
} from 'praisonai-ts';

Best Practices

MCP connections stay open until explicitly closed. Always call await mcp.close() to release resources, especially in long-running applications or when switching servers.
const mcp = new MCP('http://localhost:8080/sse');
try {
  await mcp.initialize();
  // ... use the agent
} finally {
  await mcp.close();
}
Enable debug mode when first wiring up a new MCP server. It logs the selected transport and number of tools loaded, making misconfiguration easy to spot.
const mcp = new MCP('http://localhost:8080/api', 'auto', true);
await mcp.initialize();
// Console: "Initialized MCP with 5 tools using http-streaming transport"
Auto-detection relies on the URL ending in /sse. If your server URL doesn’t follow this convention, pass the transport explicitly to avoid surprises.
// This will use HTTP-Streaming even though it's an SSE server
const wrong = new MCP('https://api.example.com/sse/v2');  // auto = http-streaming

// Correct: pass transport explicitly
const right = new MCP('https://api.example.com/sse/v2', 'sse');
Use the unified MCP class for all new integrations. MCPHttpStreaming is a standalone client without auto-detection. Only use it if you specifically need direct HTTP-Streaming without the unified interface.

MCP Security

Secure MCP connections with authentication and rate limiting

Python MCP Transports

Python equivalent — stdio, Streamable HTTP, WebSocket, SSE

Tools

Tool system overview for TypeScript agents

Agent

Agent configuration and capabilities