From 327046c568e4a8bc36033162faaea2a4f325dd1f Mon Sep 17 00:00:00 2001 From: zachary62 Date: Mon, 7 Apr 2025 17:06:11 -0400 Subject: [PATCH] add more ops --- cookbook/pocketflow-mcp/README.md | 12 +++++++----- cookbook/pocketflow-mcp/utils.py | 19 ++++++++++++++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/cookbook/pocketflow-mcp/README.md b/cookbook/pocketflow-mcp/README.md index e20cd3e..1473125 100644 --- a/cookbook/pocketflow-mcp/README.md +++ b/cookbook/pocketflow-mcp/README.md @@ -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 diff --git a/cookbook/pocketflow-mcp/utils.py b/cookbook/pocketflow-mcp/utils.py index 2bd4349..3ee9f97 100644 --- a/cookbook/pocketflow-mcp/utils.py +++ b/cookbook/pocketflow-mcp/utils.py @@ -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():