A FastAPI application that provides an API for querying an LLM agent with tools for math and weather, using LangChain and MCP (Model Consumption Protocol).
The application uses environment variables for configuration. You can set these in a .env
file in the root directory.
Copy the example file to create your own configuration:
cp .env.example .env
Then edit the .env
file to suit your needs. Here are the available configuration options:
Variable | Description | Default |
---|---|---|
API_HOST |
Host to bind the API server | 0.0.0.0 |
API_PORT |
Port for the API server | 8006 |
OPENAI_API_KEY |
Your OpenAI API key | - |
OPENAI_MODEL |
The OpenAI model to use | gpt-3.5-turbo |
MATH_SERVER_PATH |
Path to the math server script | servers/math_server.py |
WEATHER_SERVER_HOST |
Host of the weather server | localhost |
WEATHER_SERVER_PORT |
Port of the weather server | `` |
WEATHER_SERVER_TRANSPORT |
Transport protocol for the weather server | sse |
Run the API server:
python langchain_client.py
Then access the Swagger UI at: http://localhost:8006/docs
To test the agent directly without the API:
python langchain_client.py test
The weather server needs to be running separately:
python servers/weather_server.py
VSCode launch configurations are provided in the .vscode
directory:
- FastAPI Server: Runs the standard FastAPI server
- FastAPI with Weather Server: Runs the FastAPI server and starts the weather server
- FastAPI Debug Server: Runs the server with hot reloading using uvicorn directly
- Run Test Function: Runs the test function
To debug the application in VSCode:
- Open the debug panel (Ctrl+Shift+D)
- Select "FastAPI with Weather Server" from the dropdown
- Press F5 to start debugging
The application consists of:
- A FastAPI server that provides an API
- A math server that handles mathematical operations
- A weather server that provides weather information
- A LangChain agent that uses both tools
GET /
: Information about the APIGET /ask?query=...
: Ask a question to the agent (returns a streaming response)
- "What is 2 + 2?"
- "What's the weather in New York?"
- "If it's 20 degrees in New York and 15 degrees in London, what's the average temperature?"