
MCP based Math Drawing Client
Agentic MCP Client Server that solves a math task and draws output in rectangle in a drawing app.
What is Agent-MCP-Math-Draw?
Agent-MCP-Math-Draw is a Python-based application that integrates mathematical computations with visual drawing capabilities, utilizing the Gemini AI model to process math queries and generate visual outputs.
How to use Agent-MCP-Math-Draw?
To use the application, set up the MCP server, install the required dependencies, and run the client script to start processing mathematical queries and drawing results on a canvas.
Key features of Agent-MCP-Math-Draw?
- Integration with Gemini AI for solving mathematical problems.
- Visual drawing capabilities for representing mathematical results.
- Iterative problem-solving approach with a maximum of 9 iterations.
- Support for basic arithmetic and ASCII value calculations.
- Automatic drawing of results on a canvas with basic error handling.
Use cases of Agent-MCP-Math-Draw?
- Visualizing the sum of ASCII values for a word.
- Displaying complex mathematical results in a graphical format.
- Creating educational content by drawing mathematical concepts.
FAQ from Agent-MCP-Math-Draw?
- What programming language is used for this project?
The project is developed in Python.
- How do I set up the server?
Follow the installation instructions in the documentation to configure the server and dependencies.
- Can I contribute to the project?
Yes! Contributions are welcome, and you can submit issues or enhancement requests.
MCP based Math Drawing Client
The Math Drawing Client (talk2mcp_math_draw_client.py
) is a Python application that combines mathematical computations with visual drawing capabilities. It uses the Gemini AI model to process mathematical queries and generate appropriate responses.
Features
Currently Implemented Features
- Integration with Gemini AI for mathematical problem-solving
- Drawing capabilities for visual representation of mathematical results
- Iterative problem-solving approach (max 9 iterations)
- Support for basic mathematical operations and ASCII value calculations
- Automatic drawing of results on a canvas
- Basic error handling and recovery
- Simple canvas operations
- Basic text placement
- Timeout handling for operations (10-second timeout for LLM operations)
- Session management
- Tool execution with parameter validation
Server Requirements
-
MCP Server Setup:
- Python 3.x environment
- MCP server package installed
- Required server dependencies:
mcp
packagegoogle-generativeai
for AI integrationpython-dotenv
for environment management
-
Server Configuration:
- Port configuration (default: 8080)
- Memory allocation for mathematical operations
- Canvas size settings (default: 1920x1080)
- Maximum concurrent connections
- Timeout settings for operations
-
Server Tools:
- Mathematical computation engine
- Drawing canvas manager
- Tool execution handler
- Session manager
- Error handling system
Prerequisites
- Python 3.x
- Required Python packages:
python-dotenv
google-generativeai
mcp
package
Environment Setup
- Create a
.env
file in the project root - Add your Gemini API key:
GEMINI_API_KEY=your_api_key_here
Server Setup
-
Install server dependencies:
pip install mcp google-generativeai python-dotenv
-
Configure server settings in
config.json
:{ "server": { "port": 8080, "max_connections": 10, "timeout": 30, "canvas": { "width": 1920, "height": 1080 } } }
-
Start the MCP server:
python example2-3_server.py
Available Tools
Currently Implemented Tools
-
Mathematical Operations:
- Basic arithmetic operations (add, subtract, multiply, divide)
- Advanced mathematical functions (exponential, logarithmic)
- ASCII value calculations
- String manipulation and conversion
- Array operations
- Prime number calculations
- Factorial computations
- Fibonacci sequence generation
-
Drawing Tools:
- Rectangle drawing with custom dimensions
- Basic text placement with formatting
- Canvas clearing and reset
- Coordinate-based drawing
- Simple color selection
- Basic layer management
Planned Future Tools
- Enhanced Mathematical Operations
- Advanced Drawing Features:
- Analysis Tools:
- Utility Tools:
Installation
- Clone the repository
- Install required packages:
pip install python-dotenv google-generativeai mcp
- Set up your environment variables as described above
Usage
- Start the MCP server (see Server Setup section)
- Ensure the server is running and accessible
- Run the client:
python talk2mcp_math_draw_client.py
Server-Client Communication
-
Connection Protocol:
- Client establishes connection with server
- Authentication handshake
- Tool list synchronization
- Session initialization
-
Data Flow:
- Client sends mathematical queries
- Server processes requests
- Results are returned to client
- Drawing commands are executed
- Status updates are communicated
-
Error Handling:
- Connection retry mechanism
- Session recovery
- Tool execution fallback
- Resource cleanup
Example Queries
- Basic Mathematical Drawing:
Calculate the sum of ASCII values for the word "HELLO" and draw it inside a rectangle at the center of the canvas.
- Complex Mathematical Visualization:
Find the exponential values of first 5 prime numbers, sum them up, and display the result in a blue rectangle at coordinates (500, 300).
- Multiple Operations:
Convert the word "MATH" to ASCII values, calculate their sum, find the square root, and display the result in a red rectangle with white text.
- Advanced Drawing:
Draw a rectangle at (200, 200) with width 400 and height 300, then place the result of (2^10 + 3^5) inside it with centered text.
How It Works
- The client connects to the MCP server using stdio communication
- It processes mathematical queries using the Gemini AI model
- Results are automatically drawn on a canvas
- The system uses an iterative approach (max 9 iterations) to solve complex problems
- Results are displayed in a visually appealing format
- Each operation is validated against tool schemas
- Error handling and timeout mechanisms are in place
Error Handling
The client includes robust error handling for:
- API timeouts (10-second timeout for LLM operations)
- Invalid inputs
- Connection issues
- Tool execution errors
- Drawing tool failures
- Parameter validation errors
- Session management issues
Contributing
Feel free to submit issues and enhancement requests.
License
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Troubleshooting
-
Common Issues:
- Connection timeout
- Drawing tool failures
- Memory issues
- Performance problems
- Authentication errors
-
Solutions:
- Check server status
- Verify network connectivity
- Clear cache and temporary files
- Update dependencies
- Check system resources
Performance Tips
-
Optimization:
- Use appropriate canvas size
- Limit concurrent operations
- Clear unused resources
- Monitor memory usage
- Use efficient algorithms
-
Best Practices:
- Regular backups
- System updates
- Resource monitoring
- Error logging
- Performance testing