-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathagent_example.py
More file actions
86 lines (67 loc) · 2.78 KB
/
agent_example.py
File metadata and controls
86 lines (67 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Example: OrionBelt Analytics agent using LangChain / LangGraph with MCP.
Prerequisites:
pip install langchain langchain-anthropic langgraph langchain-mcp-adapters
Start OrionBelt Analytics MCP server first:
uv run server.py
Then run this script:
export ANTHROPIC_API_KEY=sk-ant-...
python agent_example.py
"""
from __future__ import annotations
import asyncio
from langchain.agents import create_agent
from langchain_mcp_adapters.client import MultiServerMCPClient
SYSTEM_PROMPT = """\
You are a database intelligence assistant powered by OrionBelt Analytics.
You help users analyze database schemas, generate ontologies, and write
safe SQL queries with fan-trap prevention.
## Workflow
1. Call connect_database to establish a connection (postgresql, snowflake,
dremio, clickhouse, or mysql).
2. Call list_schemas to discover available schemas.
3. Call analyze_schema to get the schema structure with relationships.
4. Use get_table_details for deep-dive into specific tables.
5. Call generate_ontology to create RDF/OWL ontology with SQL mappings.
6. Use validate_sql_syntax before running queries.
7. Use execute_sql_query to run validated SQL (set checklist_completed=true).
8. Use generate_chart to visualize query results.
9. Use graphrag_query_context for intelligent schema discovery via natural language.
## Rules
- Always validate SQL before execution using validate_sql_syntax.
- Set checklist_completed=true when calling execute_sql_query after validation.
- Fan-trap warnings must be resolved before executing multi-fact queries.
- Present SQL in code blocks. Explain ontology triples in plain language.
- Use GraphRAG tools for natural language schema exploration.
"""
MCP_SERVER_URL = "http://localhost:9000/mcp"
async def main() -> None:
async with MultiServerMCPClient(
{
"orionbelt-analytics": {
"url": MCP_SERVER_URL,
"transport": "streamable_http",
}
}
) as client:
tools = client.get_tools()
# Using Anthropic Claude (change to "openai:gpt-4o" for OpenAI)
agent = create_agent(
"anthropic:claude-sonnet-4-5",
tools,
prompt=SYSTEM_PROMPT,
)
# Interactive loop
print("OrionBelt Analytics Agent (type 'quit' to exit)\n")
while True:
user_input = input("You: ").strip()
if user_input.lower() in ("quit", "exit", "q"):
break
if not user_input:
continue
response = await agent.ainvoke(
{"messages": [{"role": "user", "content": user_input}]}
)
last_message = response["messages"][-1]
print(f"\nAssistant: {last_message.content}\n")
if __name__ == "__main__":
asyncio.run(main())