mgbam commited on
Commit
ba3687a
·
verified ·
1 Parent(s): c3c6e00

Update mcp_server

Browse files
Files changed (1) hide show
  1. mcp_server +31 -45
mcp_server CHANGED
@@ -1,51 +1,37 @@
1
- from mcp.server.fastmcp import FastMCP
2
- import sqlite3
3
- import json
4
 
5
- # Initialize MCP server
6
- mcp = FastMCP("EnterpriseData")
7
 
8
- # Create in-memory SQLite database
9
- conn = sqlite3.connect(":memory:", check_same_thread=False)
10
- cursor = conn.cursor()
 
 
 
 
 
 
 
11
 
12
- # Create Customers table
13
- cursor.execute("""
14
- CREATE TABLE Customers (
15
- CustomerID INTEGER PRIMARY KEY AUTOINCREMENT,
16
- Name TEXT,
17
- Region TEXT,
18
- LastOrderDate TEXT
19
- )
20
- """)
21
 
22
- # Seed sample data
23
- cursor.executemany(
24
- "INSERT INTO Customers (Name, Region, LastOrderDate) VALUES (?, ?, ?)",
25
- [
26
- ("Acme Corp", "Northeast", "2024-12-01"),
27
- ("Beta Inc", "West", "2025-06-01"),
28
- ("Gamma Co", "Northeast", "2023-09-15"),
29
- ("Delta LLC", "South", "2025-03-20"),
30
- ("Epsilon Ltd", "Northeast", "2025-07-10")
31
- ]
32
- )
33
- conn.commit()
34
 
35
- # Expose SQL tool to the AI
36
- @mcp.tool()
37
- def query_database(sql: str) -> str:
38
- """Executes raw SQL and returns JSON results."""
39
- try:
40
- cur = conn.cursor()
41
- cur.execute(sql)
42
- rows = cur.fetchall()
43
- columns = [desc[0] for desc in cur.description or []]
44
- results = [dict(zip(columns, row)) for row in rows]
45
- return json.dumps(results)
46
- except Exception as e:
47
- return json.dumps({"error": str(e)})
48
 
49
- # Start MCP server
50
- if __name__ == "__main__":
51
- mcp.run(transport="stdio")
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from mcp import StdioServerParameters
3
+ from smolagents import MCPClient, CodeAgent
4
 
 
 
5
 
6
+ def respond(prompt, history=[]):
7
+ """Send the user prompt to the MCP‑powered agent and return the reply."""
8
+ # Launch the MCP server as a subprocess (stdio transport)
9
+ params = StdioServerParameters(command="python", args=["mcp_server.py"])
10
+ with MCPClient(params) as tools:
11
+ # Replace "gpt-4" with your preferred local or API‑based LLM
12
+ agent = CodeAgent(tools=tools, model="gpt-4")
13
+ result = agent.run(prompt)
14
+ history.append((prompt, result))
15
+ return history, history
16
 
 
 
 
 
 
 
 
 
 
17
 
18
+ with gr.Blocks() as demo:
19
+ chat_history = gr.State([])
 
 
 
 
 
 
 
 
 
 
20
 
21
+ chatbot = gr.Chatbot(label="Enterprise SQL Agent")
22
+ prompt_box = gr.Textbox(
23
+ show_label=False,
24
+ placeholder="Ask questions like: 'Who are my inactive Northeast customers?'",
25
+ )
26
+ prompt_box.submit(respond, [prompt_box, chat_history], [chatbot, chat_history])
 
 
 
 
 
 
 
27
 
28
+ gr.Markdown(
29
+ """
30
+ ### Example Prompts
31
+ - Who are my Northeast customers who haven’t ordered in 6 months?
32
+ - List customers sorted by last order date.
33
+ - Find clients from the West with recent orders.
34
+ """
35
+ )
36
+
37
+ demo.launch()