FastAPI MCP Server + LangChain Client Example
@SDCalvo
关于 FastAPI MCP Server + LangChain Client Example
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
基本信息
配置
使用下面的配置,将此服务器添加到你的 MCP 客户端。
{
"mcpServers": {
"sse-mcp-and-langchain-client-example": {
"command": "uv",
"args": [
"init",
"#",
"If",
"pyproject.toml",
"doesnt exist"
]
}
}
}工具
未检测到工具
工具是从 README 中自动提取的。维护者可以在 ## Tools 标题下列出工具,即可填充这部分内容。
概览
What is FastAPI MCP Server + LangChain Client Example?
This example project demonstrates how to expose FastAPI endpoints as Model Context Protocol (MCP) tools using the fastapi-mcp library. It includes a LangChain agent client (langchain_client.py) that connects to the local FastAPI server via HTTP/SSE using langchain-mcp-adapters to discover and use the exposed tools. It is intended for learning and testing, runnable locally and connectable from MCP clients like Cursor’s agent.
How to use FastAPI MCP Server + LangChain Client Example?
Clone the repository, install uv, create a virtual environment, and install dependencies (fastapi, uvicorn, fastapi-mcp, langchain-mcp-adapters, langgraph, langchain-openai, python-dotenv). Create a .env file with your OpenAI API key. In one terminal, run uvicorn main:app --reload --port 8000. In a second terminal, run uv run python langchain_client.py. Optionally, test with the MCP Inspector via npx @modelcontextprotocol/inspector or configure Cursor using a .cursor/mcp.json with the server URL http://127.0.0.1:8000/mcp.
Key features of FastAPI MCP Server + LangChain Client Example
- Exposes FastAPI endpoints as MCP tools automatically via
fastapi-mcp. - LangChain agent discovers and calls tools through SSE transport.
- Supports Cursor editor integration via HTTP-based MCP configuration.
- Includes a ready‑to‑run example with sample endpoints and queries.
- Demonstrates optional MCP Inspector for interactive tool testing.
Use cases of FastAPI MCP Server + LangChain Client Example
- Learning how to turn a FastAPI application into an MCP server.
- Rapid prototyping of MCP tools using familiar FastAPI route definitions.
- Integrating FastAPI‑backed tools into AI agents like LangChain or Cursor.
- Testing and debugging MCP server features locally before deployment.
FAQ from FastAPI MCP Server + LangChain Client Example
What prerequisites are needed?
Python 3.10 or higher, the uv package manager, Node.js and npm (for the optional MCP Inspector), and an OpenAI API key (for the LangChain client). Git is required for cloning the repository.
How are MCP tool names determined?
MCP tool names are derived from the FastAPI route’s operation_id. If not set explicitly, FastAPI generates an automatic name (e.g., read_root__get). Setting explicit operation_id values on routes is recommended for clearer tool names.
What if a port is already in use?
Use netstat -ano | findstr "<PORT>" (Windows) to find the process ID, then taskkill /F /PID <PID> to terminate it. Sometimes a short wait or restarting the IDE is needed for the OS to fully release the port.
How do I add new routes after the server is mounted?
Routes defined after calling mcp.mount() are not automatically included. Call mcp.setup_server() again after defining the new routes to refresh the tool list.
Does this server support authentication?
Yes. fastapi-mcp supports FastAPI dependencies for authorization and includes OAuth 2 support. Basic token passthrough can be configured via mcp-remote with headers, or server‑side dependencies can be added using AuthConfig.
AI 与智能体 分类下的更多 MCP 服务器
Sequential Thinking Multi-Agent System (MAS)
FradSerAn advanced sequential thinking process using a Multi-Agent System (MAS) built with the Agno framework and served via MCP.
Perplexity MCP Server
DaInfernalCoderA Model Context Protocol (MCP) server for research and documentation assistance using Perplexity AI. Won 1st @ Cline Hackathon
MCP Claude Code
SDGLBLMCP implementation of Claude Code capabilities and more
Perplexity Ask MCP Server
ppl-aiThe official MCP server implementation for the Perplexity API Platform
meGPT - upload an author's content into an LLM
adriancoCode to process many kinds of content by an author into an MCP server
评论