FastAPI MCP Server + LangChain Client Example logo

FastAPI MCP Server + LangChain Client Example

by SDCalvo

This project demonstrates how to expose FastAPI endpoints as Model Context Protocol (MCP) tools using `fastapi-mcp`. It includes a basic LangChain agent that connects to the FastAPI server via HTTP/SSE to discover and use the exposed tools.

View on GitHub

Last updated: N/A

What is FastAPI MCP Server + LangChain Client Example?

This is an example project showcasing how to build a FastAPI server with MCP capabilities, allowing it to be used as a tool by MCP clients like the Cursor editor's agent. It also includes a LangChain client that connects to the server and uses the exposed tools.

How to use FastAPI MCP Server + LangChain Client Example?

To use this server, first clone the repository and install the necessary dependencies using uv. Then, create a .env file with your OpenAI API key. Run the FastAPI MCP server using uvicorn main:app --reload --port 8000 and the LangChain client using uv run python langchain_client.py. Optionally, test the server with the MCP Inspector.

Key features of FastAPI MCP Server + LangChain Client Example

  • Exposes FastAPI endpoints as MCP tools

  • Includes a LangChain agent client for tool discovery and usage

  • Supports HTTP/SSE communication

  • Provides a .cursor/mcp.json configuration for Cursor integration

Use cases of FastAPI MCP Server + LangChain Client Example

  • Integrating FastAPI applications with MCP-compatible clients

  • Building AI agents that can leverage FastAPI endpoints as tools

  • Testing and experimenting with the Model Context Protocol

  • Connecting Cursor editor with local APIs

FAQ from FastAPI MCP Server + LangChain Client Example

What is Model Context Protocol (MCP)?

MCP is a protocol that allows applications to expose their functionality as tools that can be used by other applications, such as AI agents.

What is fastapi-mcp?

fastapi-mcp is a library that simplifies the process of exposing FastAPI endpoints as MCP tools.

How do I install the dependencies for this project?

Use the uv pip install command as described in the README to install all required packages.

How do I configure Cursor to use this server?

Create a .cursor/mcp.json file with the server URL and enable the server in Cursor's settings.

How do I test the server?

You can use the MCP Inspector tool or the provided LangChain client to test the server's functionality.