Python MCP Server for Code Graph Extraction
Features
- **Code Relationship Discovery**: Analyze import relationships between Python files
- **Smart Code Extraction**: Extract only the most relevant code sections to stay within token limits
- **Directory Context**: Include files from the same directory to provide better context
- **Documentation Inclusion**: Always include README.md files (or variants) to provide project documentation
- **LLM-Friendly Formatting**: Format code with proper metadata for language models
- **MCP Protocol Support**: Fully compatible with the Model Context Protocol JSON-RPC standard
The `get_python_code` Tool
- Analyzes a target Python file and discovers all imported modules, classes, and functions
- Returns the complete code of the target file
- Includes code for all referenced objects from other files
- Adds additional contextual files from the same directory
- Respects token limits to avoid overwhelming language models
Installation
Environment Variables
Usage
Configuring for MCP Clients
- `TOKEN_LIMIT`: Maximum token limit for code extraction (default: 8000)
Usage Examples
Direct Function Call
Example Response (Direct Function Call)
Using the MCP Protocol
Listing Available Tools
Example Response (tools/list)
Calling get_python_code Tool
Example Response (tools/call)
Handling Errors
Example Error Response
Testing
Key Components
- **agent.py**: Contains the `get_python_code` function and custom MCP protocol handlers
- **code_grapher.py**: Implements the `CodeGrapher` class for Python code analysis
- **server.py**: Full MCP server implementation using the MCP Python SDK
- **run_server.py**: CLI tool for running the MCP server
- **examples/**: Example scripts showing how to use the MCP server and client
- **tests/**: Comprehensive test cases for all functionality
Response Format Details
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
Target File Object
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
Referenced File Object
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
Additional File Object
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
[object Object] | [object Object] | [object Object] |
Using the MCP SDK Server
Starting the Server
Using the MCP Development Mode
Claude Desktop Integration
Custom Server Deployment
Using the MCP Client
Adding Additional Tools
Model Context Protocol Integration
- **Native MCP Integration**: The original implementation in `agent.py` provides a direct JSON-RPC interface compatible with MCP.
- **MCP SDK Integration**: The new implementation in `server.py` leverages the official MCP Python SDK for a more robust and feature-rich experience.
Benefits of MCP Integration
- **Standardized Interface**: Makes your tools available to any MCP-compatible client
- **Enhanced Security**: Built-in permissions model and resource controls
- **Better LLM Integration**: Seamless integration with Claude Desktop and other LLM platforms
- **Improved Developer Experience**: Comprehensive tooling like the MCP Inspector