Skip to content

Commit aca0cfc

Browse files
author
Vicky
committed
feat: Add Langchain agent with OpenAI & A2A endpoints (refs #262)
Signed-off-by: Vicky <vicky.kuo.contact@gmail.com>
1 parent ec66a7f commit aca0cfc

File tree

11 files changed

+1332
-0
lines changed

11 files changed

+1332
-0
lines changed

agent_runtimes/langchain_agent/README.md

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# MCP Langchain Agent
12
A configurable Langchain agent that supports MCP and integrates with the MCP Gateway via streamable HTTP + Auth.
23

34
Tools can be specified as a CSV list.
@@ -11,3 +12,154 @@ Endpoints for:
1112
/list_tools
1213

1314
etc. are provided.
15+
16+
## Features update (2025-Aug-17)
17+
- **Dynamic Tool Discovery**: Automatically discovers tools from MCP Gateway (`GET /tools`)
18+
- **OpenAI-Compatible API**: Standard `/v1/chat/completions` endpoint with streaming support
19+
- **A2A Communication**: JSON-RPC `/a2a` endpoint for gateway-to-gateway communication
20+
- **Langchain Integration**: Full Langchain agent with function calling support
21+
- **Health Monitoring**: `/health`, `/ready`, `/list_tools` endpoints
22+
23+
## User Stories Implemented
24+
[x] **Dynamic Tool Discovery** - Auto-discovers from gateway or uses allowlist
25+
[x] **Dual Endpoint Exposure** - OpenAI + A2A JSON-RPC endpoints
26+
[x] **Parameterised Tool Allow-List** - `TOOLS=` environment variable
27+
[x] **Tool Schema Introspection** - JSON schema parsing and validation
28+
29+
Structure:
30+
```
31+
agent_runtimes/langchain_agent/
32+
├── app.py # FastAPI application incl. /v1/chat/completions and /a2a
33+
├── agent_langchain.py # Core Langchain agent
34+
├── mcp_client.py # MCP Gateway client
35+
├── models.py # Pydantic models
36+
├── config.py # Configuration management
37+
├── start_agent.py # Startup script
38+
├── requirements.txt # Dependencies
39+
└── README.md # This file
40+
```
41+
42+
### Configuration (env vars)
43+
- OPENAI_API_KEY – required
44+
- MCPGATEWAY_BEARER_TOKEN – JWT for the gateway
45+
46+
47+
### Installation
48+
Install dependencies:
49+
```bash
50+
cd agent_runtimes/langchain_agent
51+
pip install -r requirements.txt
52+
```
53+
54+
55+
### Quick Start
56+
1) Start the MCP Gateway (from project root):
57+
```bash
58+
make serve
59+
```
60+
61+
2) Start the Langchain Agent (in another terminal):
62+
```bash
63+
# Set environment variables
64+
export OPENAI_API_KEY=your-openai-api-key
65+
export GATEWAY_BEARER_TOKEN=$(python3 -m mcpgateway.utils.create_jwt_token -u admin --secret my-test-key)
66+
67+
# Optional: Tool allowlist (if unset, all tools discovered)
68+
export TOOLS="tool1,tool2,tool3" # Replace with actual tool names
69+
70+
python -m agent_runtimes.langchain_agent.start_agent
71+
```
72+
73+
3) Test the agent (in new terminal):
74+
75+
Basic Health Checks
76+
```bash
77+
# Health check
78+
curl http://localhost:8000/health
79+
80+
# Readiness check
81+
curl http://localhost:8000/ready
82+
83+
# List available tools
84+
curl http://localhost:8000/list_tools
85+
```
86+
87+
Basic Health Checks
88+
```bash
89+
# Basic chat completion
90+
curl -X POST http://localhost:8000/v1/chat/completions \
91+
-H "Content-Type: application/json" \
92+
-d '{
93+
"model": "gpt-4o-mini",
94+
"messages": [
95+
{"role": "user", "content": "What tools do you have available?"}
96+
]
97+
}'
98+
99+
# Streaming chat completion
100+
curl -X POST http://localhost:8000/v1/chat/completions \
101+
-H "Content-Type: application/json" \
102+
-d '{
103+
"model": "gpt-4o-mini",
104+
"messages": [
105+
{"role": "user", "content": "Hello!"}
106+
],
107+
"stream": true
108+
}'
109+
```
110+
111+
Test A2A communication with a working tool (books-search)
112+
- Step 1: Find a tool to test
113+
```bash
114+
# Get your available tools and pick one
115+
curl http://localhost:8000/list_tools | jq '.tools[0]'
116+
```
117+
118+
- Step 2: Test A2A with your tool
119+
```bash
120+
# Replace "YOUR_TOOL_NAME" with an actual tool from step 1
121+
curl -X POST http://localhost:8000/a2a \
122+
-H "Content-Type: application/json" \
123+
-d '{
124+
"jsonrpc": "2.0",
125+
"id": "1",
126+
"method": "invoke",
127+
"params": {
128+
"tool": "YOUR_TOOL_NAME",
129+
"args": {}
130+
}
131+
}'
132+
133+
# For tools that need parameters, check the tool schema:
134+
curl -X POST http://localhost:8000/a2a \
135+
-H "Content-Type: application/json" \
136+
-d '{
137+
"jsonrpc": "2.0",
138+
"id": "1",
139+
"method": "invoke",
140+
"params": {
141+
"tool": "YOUR_TOOL_NAME",
142+
"args": {
143+
"param1": "value1",
144+
"param2": "value2"
145+
}
146+
}
147+
}'
148+
```
149+
Expected Success Response:
150+
```json
151+
{
152+
"jsonrpc": "2.0",
153+
"id": "1",
154+
"result": {
155+
"success": true,
156+
"result": {
157+
"tool_id": "your-tool-id",
158+
"executed_via": "direct_rest_fallback",
159+
"status_code": 200,
160+
"result": "...actual tool output..."
161+
}
162+
}
163+
}
164+
```
165+
![alt text](image.png)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
MCP Langchain Agent Package
3+
4+
A configurable Langchain agent that supports MCP and integrates with the MCP Gateway
5+
via streamable HTTP + Auth. Exposes an OpenAI compatible API.
6+
"""
7+
from .app import app
8+
from .agent_langchain import LangchainMCPAgent
9+
from .mcp_client import MCPClient
10+
from .config import get_settings
11+
12+
__all__ = []

0 commit comments

Comments
 (0)