LangChain Integration MCP server

Use MCP tools within LangChain-powered applications.
Back to servers
Provider
rectalogic
Release date
Nov 26, 2024
Language
Python
Package
Stats
12.5K downloads
184 stars

The Model Context Protocol (MCP) server is a tool that allows language models to interact with your local filesystem and other resources in a controlled manner. It integrates with LangChain to provide tool calling capabilities through the standardized Model Context Protocol.

Installation

You can install the langchain-mcp package using pip:

pip install langchain-mcp

For better dependency management, you may want to use a tool like uv:

uv pip install langchain-mcp

Setting Up the MCP Server

To use the MCP server, you need to create an MCPToolkit instance with an MCP client session. Here's a basic example of how to set it up:

from langchain_mcp import MCPToolkit
import mcp

# Create an MCP client session
session = mcp.ClientSession(...)

# Create the toolkit and initialize it
toolkit = MCPToolkit(session)
await toolkit.initialize()

# Get the available tools
tools = toolkit.get_tools()

Environment Configuration

When using certain LLM providers like Groq, you'll need to set up your API keys as environment variables:

export GROQ_API_KEY=your_api_key_here

Using the MCP Server

Here's a complete example of how to use the MCP toolkit with LangChain:

import asyncio
import os
from langchain_groq import ChatGroq
from langchain_core.messages import HumanMessage
from langchain_mcp import MCPToolkit
import mcp

async def main():
    # Set up the LLM
    llm = ChatGroq(model_name="llama-3.1-8b-instant")
    
    # Create an MCP session
    session = mcp.ClientSession(...)
    
    # Initialize the toolkit
    toolkit = MCPToolkit(session)
    await toolkit.initialize()
    
    # Get the tools
    tools = toolkit.get_tools()
    
    # Create a message with a user query
    message = HumanMessage(content="Read and summarize the file ./LICENSE")
    
    # Invoke the LLM with the tools
    response = await llm.ainvoke([message], tools=tools)
    
    print(response.content)

if __name__ == "__main__":
    asyncio.run(main())

Running the Demo

A demo script is included that showcases how to use the MCP server to read files from your local filesystem:

export GROQ_API_KEY=xxx
uv run tests/demo.py "Read and summarize the file ./LICENSE"

When you run this command, you should see output similar to:

Secure MCP Filesystem Server running on stdio
Allowed directories: [ '/path/to/your/directory' ]
The file ./LICENSE is a MIT License agreement. It states that the software is provided "as is" without warranty...

Security Considerations

The MCP server provides access to your filesystem, so it's important to properly configure the allowed directories to prevent unauthorized access to sensitive files. By default, the demo only allows access to the current working directory.

How to add this MCP server to Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > MCP and click "Add new global MCP server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "cursor-rules-mcp": {
            "command": "npx",
            "args": [
                "-y",
                "cursor-rules-mcp"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later