import mcp
from openai import OpenAI
# Configuration
MCP_ENDPOINT = "<<MCP_ENDPOINT>>"
CORVIC_API_TOKEN = "<<YOUR_CORVIC_API_TOKEN>>"
OPENAI_API_KEY = "<<YOUR_OPENAI_API_KEY>>"
headers = {
"Authorization": f"Bearer {CORVIC_API_TOKEN}",
"Content-Type": "application/json"
}
# Initialize OpenAI client
openai_client = OpenAI(api_key=OPENAI_API_KEY)
async def query_corvic_agent(question: str):
"""Query Corvic agent using MCP."""
async with mcp.ClientSession(
transport=mcp.SSEClientTransport(MCP_ENDPOINT, headers=headers)
) as session:
await session.initialize()
query_result = await session.call_tool(
"query",
arguments={"query_content": question}
)
return query_result.content[0].text if query_result.content else ""
# Query Corvic agent
question = "Group all the data by name and find the top titles by global sales. Output the name and the total global sales in a tabular format."
corvic_response = await query_corvic_agent(question)
# Use OpenAI to format the response
openai_response = openai_client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "system",
"content": "You are a helpful assistant. Format the data analysis results clearly."
},
{
"role": "user",
"content": f"Here is the analysis from Corvic:\n\n{corvic_response}\n\nPlease format this as a clear report."
}
],
tools=[{
"type": "function",
"function": {
"name": "query_corvic",
"description": "Query Corvic agent for data analysis",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"}
}
}
}
}]
)
print(openai_response.choices[0].message.content)