Skip to main content
This example demonstrates how to integrate Composio with LangChain in Python using the Model Context Protocol (MCP) for seamless tool access.

Overview

In this example, you’ll learn how to:
  • Connect Composio to LangChain via MCP
  • Create a LangChain agent with Composio tools
  • Use async operations for better performance
  • Handle tool execution through MCP client

Prerequisites

1

Install dependencies

2

Set up environment variables

Create a .env file with your API keys:
3

Authenticate with services

Complete Example

How It Works

1

Initialize Composio Session

Create a Composio session that provides an MCP server endpoint for the user.
2

Create MCP Client

Initialize a MultiServerMCPClient that connects to Composio’s MCP server using HTTP streaming.
3

Fetch Tools

Retrieve all available tools from the MCP client. These are automatically formatted for LangChain.
4

Create Agent

Use LangChain’s create_agent function to build an agent with the MCP tools and your chosen language model.
5

Invoke Agent

Call the agent asynchronously with your query. The agent will automatically select and execute the appropriate tools.

MCP Client Configuration

string
default:"streamable_http"
The transport protocol for MCP communication. Options:
  • streamable_http: HTTP-based streaming (recommended)
  • stdio: Standard input/output (for local processes)
string
required
The MCP server URL from your Composio session
dict
required
Authentication headers for the MCP server

Expected Output

Working with Multiple MCP Servers

You can connect to multiple MCP servers simultaneously:

LangGraph Integration

For more complex workflows, use LangGraph with MCP:

Error Handling

Streaming Responses

For real-time responses:

Memory and State

Add conversation memory:

Best Practices

Use Async: Always use async/await for better performance with MCP
Clean Up Connections: Close MCP client connections when done
Handle Timeouts: Set appropriate timeouts for long-running tool operations
Stream Large Responses: Use streaming for better UX with long responses

Next Steps

CrewAI Example

Build multi-agent systems with CrewAI

Custom Tools

Create custom Python tools