FastAPI MCP Server + LangChain Client Example
About
Example project demonstrating how to expose FastAPI endpoints as Model Context Protocol (MCP) tools using `fastapi-mcp`. Includes a basic LangChain agent (`langchain_client.py`) that connects to the local FastAPI server via HTTP/SSE using `langchain-mcp-adapters` to discover and
Details
- License
- Apache-2.0 license
Explore
This section summarizes features and concepts from the fastapi-mcp documentation that we haven't implemented yet but are useful to know.
Setting up with Highlight
This MCP is not yet compatible with Highlight’s one-click setup. However, you can still use it with Highlight by following these steps:
- Download and install Highlight from highlightai.com/download
- Navigate to the plugins tab and select "Add Custom Plugin"
-
Configure the plugin with the settings below
Plugin Name
FastAPI MCP Server + LangChain Client ExampleCommand (node, npx, python, etc.)Please refer to the README for specific instructions on how to obtain API keys or other required environment variables.
- Enable "Start Automatically" if you want the plugin to start when Highlight launches
From the repository
Prerequisites
Before you begin, ensure you have the following installed: - Python: Version 3.10 or higher recommended. -uv: The Python package manager used in this project. (Installation Instructions)
- Node.js and npm: Required for npx (used to run the optional MCP Inspector). You can download Node.js (which includes npm) from nodejs.org.
- Git: For cloning the repository.
- OpenAI API Key: Required for the LangChain client example. You need to set this in a .env file.
This project demonstrates setting up a basic FastAPI application and exposing its endpoints as Model Context Protocol (MCP) tools using the fastapi-mcp library. It also includes a LangChain agent client that connects to and uses these tools, and covers configuring Cursor to connect as well.
Getting Started / How to Run
1. Clone the Repository: ``bash
git clone <your-repo-url>
cd <repo-directory>
`
2. Install uv: If you don't have it, install the uv package manager (see Project Setup below for command).
3. Set up Environment & Install Dependencies:
`bash
uv init # If pyproject.toml doesn't exist
uv venv # Create virtual environment (.venv)
# Install all project dependencies
uv pip install fastapi "uvicorn[standard]" fastapi-mcp langchain-mcp-adapters langgraph langchain-openai python-dotenv
`
4. Create .env File: Create a file named .env in the project root directory and add your OpenAI API key:
`dotenv
OPENAI_API_KEY=your_openai_api_key_here
`
5. Run the FastAPI MCP Server: Open a terminal and run:
`bash
uvicorn main:app --reload --port 8000
`
Keep this terminal running.
_(Alternatively, you can use the Python: FastAPI MCP debug configuration defined in .vscode/launch.json within VS Code / Cursor to run the server with the debugger attached.)_
6. Run the LangChain Client: Open a _second_ terminal and run:
`bash
uv run python langchain_client.py
`
The client will connect to the server, discover tools, and run a query using the agent.
7. (Optional) Test Server with MCP Inspector: Before running the LangChain client, or for more direct testing, you can use the official MCP Inspector tool:
- Ensure the FastAPI server is running (Step 5).
- Open another terminal and run: npx @modelcontextprotocol/inspector
_(npx comes with Node.js/npm. If this command fails, ensure Node.js is installed and accessible in your PATH.)_
- In the inspector UI, connect to your server URL: http://127.0.0.1:8000/mcp
- Navigate to "Tools", click "List Tools" to see read_root__get and greet_user_greet__name__get.
- Select a tool, fill parameters (e.g., name for greet_user), and click "Run Tool".
8. (Optional) Test greet_user with LangChain Client: The greet_user endpoint and the corresponding test query (query2) in langchain_client.py are currently active. Simply run the LangChain client (Step 6) and observe the second part of its execution where it should attempt to greet the user 'LangChain'.
- _(If you want to disable this test, comment out the @app.get("/greet/{name}") endpoint in main.py and the query2 section in langchain_client.py)_
Project Setup
1. Package Manager: We used uv, a fast Python package installer and resolver written in Rust.
- Installation (Windows PowerShell): irm https://astral.sh/uv/install.ps1 | iex
- Project Initialization: uv init (Creates pyproject.toml)
- Virtual Environment: uv venv (Creates and manages .venv)
2. Dependencies: Installed using uv:
`bash
# Specific commands used during development (covered by the combined install in Getting Started):
# uv pip install fastapi "uvicorn[standard]" fastapi-mcp
# uv pip install langchain-mcp-adapters langgraph langchain-openai python-dotenv
`
This installs FastAPI, the Uvicorn ASGI server, fastapi-mcp, LangChain components, and python-dotenv into the .venv` virtual environment.- MCP tool names are derived from the FastAPI route's operation_id.
- If not specified, FastAPI generates one automatically (e.g., read_user_users__user_id__get).
- It's recommended to set explicit operation_ids on FastAPI routes for clearer MCP tool names:
``python
@app.get("/users/{user_id}", operation_id="get_user_info")
async def read_user(user_id: int):
- If FastAPI routes are added _after_ mcp.mount() is called, they won't be automatically included.mcp.setup_server()
- Solution: Call again after defining the new routes.``python
app = FastAPI()
mcp = FastApiMCP(app)
mcp.mount()
@app.get("/new/endpoint", operation_id="new_tool")
async def new_endpoint(): ...
Claude Desktop / Cursor
Paste into your MCP client config file to install this server.
{
"mcpServers": {
"fastapi mcp server + langchain client example": {
"sse-mcp-and-langchain-client-example": {
"command": "uv",
"args": [
"init",
"#",
"If",
"pyproject.toml",
"doesnt exist"
]
}
}
}
}
McpServers
{
"sse-mcp-and-langchain-client-example": {
"command": "uv",
"args": [
"init",
"#",
"If",
"pyproject.toml",
"doesnt exist"
]
}
}
fastapi-mcp. Includes a basic LangChain agent (langchain_client.py) that connects to the local FastAPI server via HTTP/SSE using langchain-mcp-adapters to discover and use the exposed tools.
Prerequisites
Before you begin, ensure you have the following installed: - Python: Version 3.10 or higher recommended. -uv: The Python package manager used in this project. (Installation Instructions)
- Node.js and npm: Required for npx (used to run the optional MCP Inspector). You can download Node.js (which includes npm) from nodejs.org.
- Git: For cloning the repository.
- OpenAI API Key: Required for the LangChain client example. You need to set this in a .env file.
This project demonstrates setting up a basic FastAPI application and exposing its endpoints as Model Context Protocol (MCP) tools using the fastapi-mcp library. It also includes a LangChain agent client that connects to and uses these tools, and covers configuring Cursor to connect as well.
Getting Started / How to Run
1. Clone the Repository: ``bash
git clone <your-repo-url>
cd <repo-directory>
`
2. Install uv: If you don't have it, install the uv package manager (see Project Setup below for command).
3. Set up Environment & Install Dependencies:
`bash
uv init # If pyproject.toml doesn't exist
uv venv # Create virtual environment (.venv)
# Install all project dependencies
uv pip install fastapi "uvicorn[standard]" fastapi-mcp langchain-mcp-adapters langgraph langchain-openai python-dotenv
`
4. Create .env File: Create a file named .env in the project root directory and add your OpenAI API key:
`dotenv
OPENAI_API_KEY=your_openai_api_key_here
`
5. Run the FastAPI MCP Server: Open a terminal and run:
`bash
uvicorn main:app --reload --port 8000
`
Keep this terminal running.
_(Alternatively, you can use the Python: FastAPI MCP debug configuration defined in .vscode/launch.json within VS Code / Cursor to run the server with the debugger attached.)_
6. Run the LangChain Client: Open a _second_ terminal and run:
`bash
uv run python langchain_client.py
`
The client will connect to the server, discover tools, and run a query using the agent.
7. (Optional) Test Server with MCP Inspector: Before running the LangChain client, or for more direct testing, you can use the official MCP Inspector tool:
- Ensure the FastAPI server is running (Step 5).
- Open another terminal and run: npx @modelcontextprotocol/inspector
_(npx comes with Node.js/npm. If this command fails, ensure Node.js is installed and accessible in your PATH.)_
- In the inspector UI, connect to your server URL: http://127.0.0.1:8000/mcp
- Navigate to "Tools", click "List Tools" to see read_root__get and greet_user_greet__name__get.
- Select a tool, fill parameters (e.g., name for greet_user), and click "Run Tool".
8. (Optional) Test greet_user with LangChain Client: The greet_user endpoint and the corresponding test query (query2) in langchain_client.py are currently active. Simply run the LangChain client (Step 6) and observe the second part of its execution where it should attempt to greet the user 'LangChain'.
- _(If you want to disable this test, comment out the @app.get("/greet/{name}") endpoint in main.py and the query2 section in langchain_client.py)_
Goal
To build a simple FastAPI server with MCP capabilities for learning and testing purposes, runnable locally and connectable from MCP clients like the Cursor editor's agent.
Project Setup
1. Package Manager: We used uv, a fast Python package installer and resolver written in Rust.
- Installation (Windows PowerShell): irm https://astral.sh/uv/install.ps1 | iex
- Project Initialization: uv init (Creates pyproject.toml)
- Virtual Environment: uv venv (Creates and manages .venv)
2. Dependencies: Installed using uv:
`bash
# Specific commands used during development (covered by the combined install in Getting Started):
# uv pip install fastapi "uvicorn[standard]" fastapi-mcp
# uv pip install langchain-mcp-adapters langgraph langchain-openai python-dotenv
`
This installs FastAPI, the Uvicorn ASGI server, fastapi-mcp, LangChain components, and python-dotenv into the .venv virtual environment.
Application (
main.py)
A simple FastAPI app was created with endpoints:
- /: Returns a welcome message.
- /greet/{name}: Returns a personalized greeting (currently commented out).
Crucially, the fastapi-mcp integration happens after the FastAPI route definitions:
``python
from fastapi import FastAPI
from fastapi_mcp import FastApiMCP
app = FastAPI(...)Sign in to leave a review
Use Google, GitHub, or an email account so ratings stay tied to real people.
No reviews posted yet.



