Skip to main content

How to Use MCP Tools with LangChain Agents

This document provides a developer-focused guide on how to integrate Vinci’s MCP server with your own LangChain agent. It’s structured as a cookbook with recipes to guide you through the implementation.

Overview

This guide shows how you can connect your LangChain agent to Vinci’s MCP (Multi-tool Control Plane) server. This enables your agent to dynamically discover and utilize our powerful external tools, extending its capabilities beyond simple chat.

Prerequisites

Before you begin, make sure you have the necessary packages installed.
bash
pip install langchain langchain-google-genai fastmcp python-dotenv

Configuration

To run the examples in this guide, you need to configure the following environment variables in your .env file.
# .env

# Vinci MCP server URL
VINCI_MCP_URL=https://vinci-backend2-serving-382403086889.us-central1.run.app/agent/mcp

# Your Vinci User ID (from Vinci Dashboard → API)
VINCI_USER_ID=your-user-id

# Your Google Gemini API key
GEMINI_API_KEY=your-google-api-key

You can find your USER_ID on the Vinci Dashboard API page.

Recipes

How to Fetch Tools from the MCP Server

This recipe shows how to connect to Vinci’s MCP server, retrieve the available tools, and print their names and descriptions. This is the foundational step for integrating Vinci’s tools into any LangChain agent. Code:
# agents/vinci_mcp_tools.py

import os
import asyncio
from langchain_mcp_adapters.client import MultiServerMCPClient
from dotenv import load_dotenv
load_dotenv()

async def get_vinci_tools():
    """
    Connects to Vinci's MCP server using the MultiServerMCPClient
    and retrieves available tools as LangChain-compatible tools.
    """
    server_url = os.getenv("VINCI_MCP_URL")
    user_id = os.getenv("VINCI_USER_ID")

    if not server_url or not user_id:
        raise EnvironmentError("Missing VINCI_MCP_URL or VINCI_USER_ID in environment.")

    client = MultiServerMCPClient(
        {
            # Vinci’s MCP server
            "vinci": {
                "transport": "streamable_http",
                "url": server_url,
                "headers": {"X-User-ID": user_id},
            },
        }
    )

    # Fetch tools from Vinci’s MCP server
    tools = await client.get_tools()
    return tools

async def main():
    """
    Fetches tools and prints their details.
    """
    tools = await get_vinci_tools()
    for tool in tools:
        print(f"Tool Name: {tool.name}, Description: {tool.description}")
    print(f"Retrieved {len(tools)} tools from Vinci MCP server.")

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

How to Integrate Tools with a LangChain Agent

Once you can fetch the tools, you can integrate them into a LangChain agent. This example demonstrates how to build an agent that can use the Vinci tools to perform tasks. Code:
from langchain.agents import create_agent
from agents.vinci_mcp_tools import get_vinci_tools
import asyncio
from langchain_google_genai import ChatGoogleGenerativeAI
import os


MODEL_NAME = "gemini-2.5-flash"
tools = asyncio.run(get_vinci_tools())
for tool in tools:
    tool.name = tool.name.replace(" ", "_")
    print(f"Tool Name: '{tool.name}'")
print(f"Retrieved {len(tools)} tools from Vinci MCP server.")
llm = ChatGoogleGenerativeAI(
    model=MODEL_NAME,
    google_api_key=os.getenv("GEMINI_API_KEY"),
    temperature=0.7,
)

agent = create_agent(
    llm,
    tools
)
response = asyncio.run(agent.ainvoke(
    {"messages": [{"role": "user", "content": "generate a cat image"}]}
))
print(response)

Conclusion

By following these recipes, you can seamlessly integrate Vinci’s powerful MCP tools into your own LangChain agents. This allows you to build sophisticated, tool-augmented applications with ease. We encourage you to experiment with the available tools and explore the creative possibilities they unlock.