| import pathlib |
| import os |
| import gradio as gr |
| from mcp import StdioServerParameters |
| from smolagents import MCPClient |
| from smolagents.agents import OpenAIAgent |
|
|
| SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py") |
|
|
|
|
| def respond(message: str, history: list): |
| """Send the user prompt to an OpenAI-backed agent with MCP tools.""" |
| |
| params = StdioServerParameters(command="python", args=[str(SERVER_PATH)]) |
|
|
| |
| with MCPClient(params) as tools: |
| |
| agent = OpenAIAgent( |
| tools=tools, |
| model=os.getenv("OPENAI_MODEL", "gpt-4o-preview"), |
| temperature=0.3, |
| ) |
| answer = agent.run(message) |
|
|
| |
| history.append({"role": "user", "content": message}) |
| history.append({"role": "assistant", "content": answer}) |
| return history, history |
|
|
|
|
| with gr.Blocks(title="Enterprise SQL Agent") as demo: |
| chat_state = gr.State([]) |
| chatbot = gr.Chatbot(type="messages", label="Enterprise SQL Agent") |
|
|
| textbox = gr.Textbox( |
| placeholder="Ask: Who are my inactive Northeast customers?", show_label=False |
| ) |
| textbox.submit(respond, [textbox, chat_state], [chatbot, chat_state]) |
|
|
| gr.Markdown( |
| """ |
| ### Example Prompts |
| - Who are my Northeast customers who haven’t ordered in 6 months? |
| - List customers sorted by last order date. |
| - Find clients from the West with recent orders. |
| |
| _Built with MCP, smolagents, Gradio, and OpenAI_ |
| """ |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|