mgbam commited on
Commit
f37945a
·
verified ·
1 Parent(s): 833ef84

Update mcp_server

Browse files
Files changed (1) hide show
  1. mcp_server +41 -37
mcp_server CHANGED
@@ -1,37 +1,41 @@
1
- import gradio as gr
2
- from mcp import StdioServerParameters
3
- from smolagents import MCPClient, CodeAgent
4
-
5
-
6
- def respond(message, history):
7
- """Send the message to the MCP-powered agent and return its answer."""
8
- params = StdioServerParameters(command="python", args=["mcp_server.py"])
9
- with MCPClient(params) as tools:
10
- agent = CodeAgent(tools=tools, model="gpt-4") # swap for your model
11
- answer = agent.run(message)
12
- history.append((message, answer))
13
- return history, history
14
-
15
-
16
- with gr.Blocks() as demo:
17
- chat_state = gr.State([])
18
- chatbot = gr.Chatbot(label="Enterprise SQL Agent")
19
-
20
- textbox = gr.Textbox(
21
- show_label=False,
22
- placeholder="Ask: Who are my inactive Northeast customers?",
23
- )
24
- textbox.submit(respond, [textbox, chat_state], [chatbot, chat_state])
25
-
26
- gr.Markdown(
27
- """
28
- ### Example Prompts
29
- - Who are my Northeast customers who haven’t ordered in 6 months?
30
- - List customers sorted by last order date.
31
- - Find clients from the West with recent orders.
32
- """
33
- )
34
-
35
- gr.Markdown("Built with MCP, smolagents, and Gradio")
36
-
37
- demo.launch()
 
 
 
 
 
1
+ from mcp.server.fastmcp import FastMCP
2
+ import sqlite3, json
3
+
4
+ mcp = FastMCP("EnterpriseData")
5
+
6
+ conn = sqlite3.connect(":memory:", check_same_thread=False)
7
+ cur = conn.cursor()
8
+
9
+ cur.execute("""
10
+ CREATE TABLE Customers (
11
+ CustomerID INTEGER PRIMARY KEY AUTOINCREMENT,
12
+ Name TEXT,
13
+ Region TEXT,
14
+ LastOrderDate TEXT
15
+ )
16
+ """)
17
+ cur.executemany(
18
+ "INSERT INTO Customers (Name, Region, LastOrderDate) VALUES (?, ?, ?)",
19
+ [
20
+ ("Acme Corp", "Northeast", "2024-12-01"),
21
+ ("Beta Inc", "West", "2025-06-01"),
22
+ ("Gamma Co", "Northeast", "2023-09-15"),
23
+ ("Delta LLC", "South", "2025-03-20"),
24
+ ("Epsilon Ltd", "Northeast", "2025-07-10")
25
+ ]
26
+ )
27
+ conn.commit()
28
+
29
+ @mcp.tool()
30
+ def query_database(sql: str) -> str:
31
+ """Run a SQL query and return JSON."""
32
+ try:
33
+ cur.execute(sql)
34
+ cols = [d[0] for d in cur.description or []]
35
+ rows = [dict(zip(cols, r)) for r in cur.fetchall()]
36
+ return json.dumps(rows)
37
+ except Exception as e:
38
+ return json.dumps({"error": str(e)})
39
+
40
+ if __name__ == "__main__":
41
+ mcp.run(transport="stdio")