Spaces:
Sleeping
Sleeping
client
Browse files- mcp-flight-agent-http.py +62 -0
mcp-flight-agent-http.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import dspy
|
3 |
+
import asyncio
|
4 |
+
import mlflow
|
5 |
+
|
6 |
+
from mcp import ClientSession
|
7 |
+
from mcp.client.streamable_http import streamablehttp_client
|
8 |
+
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
|
11 |
+
load_dotenv()
|
12 |
+
|
13 |
+
lm = dspy.LM(
|
14 |
+
model='openai/gpt-4o-mini',
|
15 |
+
temperature=0,
|
16 |
+
api_key=os.environ['OPENAI_API_KEY'],
|
17 |
+
api_base=os.environ['OPENAI_BASE_URL']
|
18 |
+
)
|
19 |
+
|
20 |
+
mcp_url = "https://pgurazada1-airline-ops-mcp-server.hf.space/mcp/"
|
21 |
+
# mcp_url = "http://localhost:8000/mcp" # local server testing
|
22 |
+
|
23 |
+
dspy.configure(lm=lm)
|
24 |
+
mlflow.dspy.autolog()
|
25 |
+
mlflow.set_experiment('flight-agent-http')
|
26 |
+
|
27 |
+
|
28 |
+
class DSPyAirlineCustomerService(dspy.Signature):
|
29 |
+
"""
|
30 |
+
You are an airline customer service agent.
|
31 |
+
You are given a list of tools to handle user requests.
|
32 |
+
You should decide the right tool to use in order to fulfill users' requests.
|
33 |
+
"""
|
34 |
+
|
35 |
+
user_request: str = dspy.InputField()
|
36 |
+
process_result: str = dspy.OutputField(
|
37 |
+
desc=(
|
38 |
+
"Message that summarizes the process result, and the information users need, "
|
39 |
+
"e.g., the confirmation_number if it's a flight booking request."
|
40 |
+
)
|
41 |
+
)
|
42 |
+
|
43 |
+
async def run(user_request):
|
44 |
+
async with streamablehttp_client(url=mcp_url) as (read, write, _):
|
45 |
+
async with ClientSession(read, write) as session:
|
46 |
+
# Initialize the connection
|
47 |
+
await session.initialize()
|
48 |
+
# List available tools
|
49 |
+
tools = await session.list_tools()
|
50 |
+
|
51 |
+
# Convert MCP tools to DSPy tools
|
52 |
+
dspy_tools = []
|
53 |
+
for tool in tools.tools:
|
54 |
+
dspy_tools.append(dspy.Tool.from_mcp_tool(session, tool))
|
55 |
+
|
56 |
+
# Create the agent
|
57 |
+
react_agent = dspy.ReAct(DSPyAirlineCustomerService, tools=dspy_tools)
|
58 |
+
|
59 |
+
output = await react_agent.acall(user_request=user_request)
|
60 |
+
print(output.process_result)
|
61 |
+
|
62 |
+
asyncio.run(run("please help me book a flight from SFO to JFK on 09/01/2025, my name is Adam"))
|