From f61ada792833d256ff77aebd2fbf887f4eee308d Mon Sep 17 00:00:00 2001 From: Pavan Kumar <66913595+ascender1729@users.noreply.github.com> Date: Sun, 20 Apr 2025 16:40:40 +0000 Subject: [PATCH] fix(mcp): resolve indentation error in mcp_client.py --- examples/mcp_example/mcp_client.py | 159 +++++++++++++++-------------- 1 file changed, 80 insertions(+), 79 deletions(-) diff --git a/examples/mcp_example/mcp_client.py b/examples/mcp_example/mcp_client.py index 71b29cdc..bf83d2d4 100644 --- a/examples/mcp_example/mcp_client.py +++ b/examples/mcp_example/mcp_client.py @@ -1,83 +1,84 @@ - from swarms import Agent - from swarms.tools.mcp_integration import MCPServerSseParams - from loguru import logger - - # Comprehensive math prompt that encourages proper JSON formatting - MATH_AGENT_PROMPT = """ - You are a helpful math calculator assistant. - - Your role is to understand natural language math requests and perform calculations. - When asked to perform calculations: - - 1. Determine the operation (add, multiply, or divide) - 2. Extract the numbers from the request - 3. Use the appropriate math operation tool - - FORMAT YOUR TOOL CALLS AS JSON with this format: - {"tool_name": "add", "a": , "b": } - or - {"tool_name": "multiply", "a": , "b": } - or - {"tool_name": "divide", "a": , "b": } - - Always respond with a tool call in JSON format first, followed by a brief explanation. - """ - - def initialize_math_system(): - """Initialize the math agent with MCP server configuration.""" - # Configure the MCP server connection - math_server = MCPServerSseParams( - url="http://0.0.0.0:8000", - headers={"Content-Type": "application/json"}, - timeout=5.0, - sse_read_timeout=30.0 - ) - - # Create the agent with the MCP server configuration - math_agent = Agent( - agent_name="Math Assistant", - agent_description="Friendly math calculator", - system_prompt=MATH_AGENT_PROMPT, - max_loops=1, - mcp_servers=[math_server], # Pass MCP server config as a list - model_name="gpt-3.5-turbo", - verbose=True # Enable verbose mode to see more details - ) - - return math_agent - - def main(): + +from swarms import Agent +from swarms.tools.mcp_integration import MCPServerSseParams +from loguru import logger + +# Comprehensive math prompt that encourages proper JSON formatting +MATH_AGENT_PROMPT = """ +You are a helpful math calculator assistant. + +Your role is to understand natural language math requests and perform calculations. +When asked to perform calculations: + +1. Determine the operation (add, multiply, or divide) +2. Extract the numbers from the request +3. Use the appropriate math operation tool + +FORMAT YOUR TOOL CALLS AS JSON with this format: +{"tool_name": "add", "a": , "b": } +or +{"tool_name": "multiply", "a": , "b": } +or +{"tool_name": "divide", "a": , "b": } + +Always respond with a tool call in JSON format first, followed by a brief explanation. +""" + +def initialize_math_system(): + """Initialize the math agent with MCP server configuration.""" + # Configure the MCP server connection + math_server = MCPServerSseParams( + url="http://0.0.0.0:8000", + headers={"Content-Type": "application/json"}, + timeout=5.0, + sse_read_timeout=30.0 + ) + + # Create the agent with the MCP server configuration + math_agent = Agent( + agent_name="Math Assistant", + agent_description="Friendly math calculator", + system_prompt=MATH_AGENT_PROMPT, + max_loops=1, + mcp_servers=[math_server], # Pass MCP server config as a list + model_name="gpt-3.5-turbo", + verbose=True # Enable verbose mode to see more details + ) + + return math_agent + +def main(): + try: + logger.info("Initializing math system...") + math_agent = initialize_math_system() + + print("\nMath Calculator Ready!") + print("Ask me any math question!") + print("Examples: 'what is 5 plus 3?' or 'can you multiply 4 and 6?'") + print("Type 'exit' to quit\n") + + while True: try: - logger.info("Initializing math system...") - math_agent = initialize_math_system() - - print("\nMath Calculator Ready!") - print("Ask me any math question!") - print("Examples: 'what is 5 plus 3?' or 'can you multiply 4 and 6?'") - print("Type 'exit' to quit\n") - - while True: - try: - query = input("What would you like to calculate? ").strip() - if not query: - continue - if query.lower() == 'exit': - break - - logger.info(f"Processing query: {query}") - result = math_agent.run(query) - print(f"\nResult: {result}\n") - - except KeyboardInterrupt: - print("\nGoodbye!") - break - except Exception as e: - logger.error(f"Error processing query: {e}") - print(f"Sorry, there was an error: {str(e)}") + query = input("What would you like to calculate? ").strip() + if not query: + continue + if query.lower() == 'exit': + break + + logger.info(f"Processing query: {query}") + result = math_agent.run(query) + print(f"\nResult: {result}\n") + except KeyboardInterrupt: + print("\nGoodbye!") + break except Exception as e: - logger.error(f"System initialization error: {e}") - print(f"Failed to start the math system: {str(e)}") + logger.error(f"Error processing query: {e}") + print(f"Sorry, there was an error: {str(e)}") + + except Exception as e: + logger.error(f"System initialization error: {e}") + print(f"Failed to start the math system: {str(e)}") - if __name__ == "__main__": - main() \ No newline at end of file +if __name__ == "__main__": + main()