add more ops

This commit is contained in:
zachary62 2025-04-07 17:06:11 -04:00
parent 84d1478b66
commit 327046c568
2 changed files with 25 additions and 6 deletions

View File

@ -4,9 +4,10 @@ This project shows how to build an agent that performs addition using PocketFlow
## Features
- Addition operations through a simple terminal interface
- Mathematical operation tools through a simple terminal interface
- Integration with Model Context Protocol (MCP)
- Comparison between MCP and direct function calling
- **Simple toggle** between MCP and local function calling
## How to Run
@ -26,9 +27,10 @@ This project shows how to build an agent that performs addition using PocketFlow
To compare both approaches, this demo provides local function alternatives that don't require MCP:
- To use traditional function calling instead of MCP, replace:
- `get_tools()` with `local_get_tools()`
- `call_tool()` with `local_call_tool()`
- **Toggle with a simple flag:** Set `MCP = True` or `MCP = False` at the top of `utils.py` to switch between MCP and local implementations.
- No code changes needed! The application automatically uses either:
- MCP server tools when `MCP = True`
- Local function implementations when `MCP = False`
This allows you to see the difference between the two approaches while keeping the same workflow.
@ -53,7 +55,7 @@ flowchart LR
The agent uses PocketFlow to create a workflow where:
1. It takes user input about numbers
2. Connects to the MCP server for addition operations
2. Connects to the MCP server for mathematical operations (or uses local functions based on the `MCP` flag)
3. Returns the result
## Files

View File

@ -4,6 +4,9 @@ import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
# Global flag to control whether to use MCP or local implementation
MCP = False
def call_llm(prompt):
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "your-api-key"))
r = client.chat.completions.create(
@ -12,7 +15,14 @@ def call_llm(prompt):
)
return r.choices[0].message.content
def get_tools(server_script_path):
def get_tools(server_script_path=None):
"""Get available tools, either from MCP server or locally based on MCP global setting."""
if MCP:
return mcp_get_tools(server_script_path)
else:
return local_get_tools(server_script_path)
def mcp_get_tools(server_script_path):
"""Get available tools from an MCP server.
"""
async def _get_tools():
@ -97,6 +107,13 @@ def local_get_tools(server_script_path=None):
return [DictObject(tool) for tool in tools]
def call_tool(server_script_path=None, tool_name=None, arguments=None):
"""Call a tool, either from MCP server or locally based on MCP global setting."""
if MCP:
return mcp_call_tool(server_script_path, tool_name, arguments)
else:
return local_call_tool(server_script_path, tool_name, arguments)
def mcp_call_tool(server_script_path=None, tool_name=None, arguments=None):
"""Call a tool on an MCP server.
"""
async def _call_tool():