Update mcp_server
Browse files- mcp_server +31 -45
mcp_server
CHANGED
|
@@ -1,51 +1,37 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
# Initialize MCP server
|
| 6 |
-
mcp = FastMCP("EnterpriseData")
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 23 |
-
|
| 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 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 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 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|