Are you the author? Sign in to claim
MCP server for automatic Python type inference and checking using Pyrefly
A Model Context Protocol (MCP) server that provides automatic Python type annotation using Pyrefly. This server enables LLMs and AI coding assistants to analyze Python code, add type annotations, and perform type checking seamlessly.
The Model Context Protocol (MCP) is an open standard that enables AI assistants and language models to securely access external data sources and tools. MCP servers act as bridges between AI systems and various resources, providing structured access to information and capabilities.
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ LLM/AI Client │◄──►│ MCP Server │◄──►│ External Tools │
│ (e.g. Claude) │ │ (This Project) │ │ (Pyrefly) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
MCP servers can provide:
This allows AI assistants to:
This MCP server provides comprehensive Python type annotation capabilities:
pip install uv or see uv installation guide# Clone or download this repository
git clone https://github.com/your-username/mcp-pyrefly-autotype.git
cd mcp-pyrefly-autotype
# Install dependencies with uv
uv sync
# For development (includes dev dependencies)
uv sync --dev
# Alternative: traditional pip install
# pip install -e .
# pip install -e ".[dev]"
The server can be run directly or integrated with MCP-compatible clients:
# Run directly (for testing)
uv run python -m mcp_pyrefly_autotype.server
# Or use the installed script (after uv sync)
uv run mcp-pyrefly-autotype
# Alternative: activate virtual environment first
uv shell
python -m mcp_pyrefly_autotype.server
Add to your Claude Desktop configuration:
{
"mcpServers": {
"pyrefly-autotype": {
"command": "uv",
"args": ["run", "python", "-m", "mcp_pyrefly_autotype.server"],
"env": {}
}
}
}
{
"servers": {
"pyrefly-autotype": {
"type": "stdio",
"command": "uv",
"args": [
"run",
"mcp-pyrefly-autotype"
]
}
},
"inputs": []
}
.vscode/mcp.json file shown above in the root of your workspace.pyrefly-autotype. Status should be Running. If not:
uv is installed and on PATH, and that uv sync has been run.pyrefly-autotype listed. If it’s missing, check that:
Try these prompts in a new Copilot Chat tab. Copilot will call the server’s tools for you.
simple_untyped.py (detailed=true), then add types to it, and finally type check it. Repeat add→check up to 3 times until type check passes.”example_untyped.py for missing annotations, add types with a backup, and run a type check. Summarize changes and remaining warnings.”simple_untyped.py: add_types_to_file → type_check_file, refining up to 3 rounds.”Expected outcomes:
analyze_python_file, add_types_to_file, type_check_file.analyze_python_fileAnalyze a Python file for missing type annotations.
Parameters:
file_path (required): Path to the Python filedetailed (optional): Include detailed analysis informationExample:
# LLM can request:
# "Analyze the file 'src/utils.py' for type annotation needs"
add_types_to_fileAdd type annotations to a Python file using Pyrefly (this invokes pyrefly autotype under the hood).
Parameters:
file_path (required): Path to the Python filebackup (optional): Create backup before modifying (default: true)Example:
# LLM can request:
# "Add type annotations to 'src/models.py'"
type_check_fileRun type checking on a Python file using Pyrefly.
Parameters:
file_path (required): Path to the Python fileExample:
# LLM can request:
# "Type check the file 'src/api.py' and report any errors"
get_project_contextGet project-wide type information for better inference.
Parameters:
project_path (required): Path to the project directoryExample:
# LLM can request:
# "Analyze the project structure for type annotation opportunities"
analyze_typing_needsGenerate analysis prompts for type annotation needs.
type_improvement_planCreate a comprehensive plan for improving type coverage in a project.
# LLM workflow:
# 1. "Analyze 'calculator.py' for type needs"
# 2. "Add types to 'calculator.py'"
# 3. "Type check 'calculator.py' and report results"
# LLM workflow:
# 1. "Get project context for '/my-project'"
# 2. "Create a type improvement plan for the project"
# 3. "Prioritize files for type annotation"
# For completely untyped files:
# 1. "Analyze 'legacy_code.py' - it has no types at all"
# 2. "Add types to 'legacy_code.py'"
# 3. "Check for type errors and suggest corrections"
The server respects Pyrefly's configuration. You can configure Pyrefly in your project using either:
pyrefly.toml file in your project root:# Files to include in type checking
project-includes = ["src/**/*.py"]
# Files to exclude from type checking
project-excludes = ["tests/**", "**/__pycache__/**"]
# Python version to assume
python-version = "3.12"
# How to handle untyped function definitions
untyped-def-behavior = "check-and-infer-return-type"
# Configure specific error types
[errors]
# Enable/disable specific error types
bad-assignment = true
missing-return-type = true
pyproject.toml file under the [tool.pyrefly] section:[tool.pyrefly]
# Files to include in type checking
project-includes = ["src/**/*.py"]
# Files to exclude from type checking
project-excludes = ["tests/**", "**/__pycache__/**"]
# Python version and platform
python-version = "3.12"
python-platform = "linux"
# Type checking behavior
untyped-def-behavior = "check-and-infer-return-type"
ignore-missing-imports = ["requests.*", "numpy.*"]
# Error configuration
[tool.pyrefly.errors]
bad-assignment = true
missing-return-type = true
See the Pyrefly Configuration Documentation for all available options.
# Run all tests
uv run pytest tests/
# Run with coverage
uv run pytest tests/ --cov=mcp_pyrefly_autotype
# Run specific test
uv run python tests/test_server.py
# Test server functions directly
uv run python test_direct.py
# Run demo workflow
uv run python test_demo.py
The project includes several test files to verify functionality:
tests/test_server.py - Comprehensive test suite with mocked pyrefly callstest_direct.py - Direct testing of server functions with real pyreflytest_demo.py - Interactive demo showing the complete workflowsimple_untyped.py - Example file for testing type annotationTo test the server end-to-end:
# 1. Test with a simple untyped file
uv run python test_demo.py
# 2. Test server functions directly
uv run python test_direct.py
# 3. Run the MCP server (for client integration)
uv run python -m mcp_pyrefly_autotype.server
# Format code
uv run black src/ tests/
# Lint code
uv run ruff check src/ tests/
# Type check
uv run pyrefly check src/
git checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)This project is licensed under the MIT License - see the LICENSE file for details.
For questions and support:
This MCP server bridges the gap between AI assistants and Python type annotation tools, enabling seamless integration of type enhancement workflows in AI-powered development environments.
The sample_queries/ directory contains ready-to-use prompt templates you can paste into your AI client (VS Code with Copilot MCP or Claude Desktop) to drive the server effectively:
sample_queries/PromptWithTools.md — A compact “agent loop” prompt that instructs the assistant to use the available tools (add_types_to_file, type_check_file, and optionally get_project_context) and iterate up to 3 refinement rounds. Great for single-file or small feature work.sample_queries/LargeUntypedCodebase.md — A batch-oriented workflow for incrementally typing a large, mostly-untyped repo. It includes planning, per-file refine loops, batch gates, and progress tracking guidance.How to use with VS Code + Copilot:
pyrefly-autotype MCP server appears under Tools (see instructions above).sample_queries/, copy the prompt, and paste it into Copilot Chat.How to use with Claude Desktop:
Tip: Start with PromptWithTools.md on a single file (e.g., simple_untyped.py) to see the full add → check → refine flow end to end. Then progress to LargeUntypedCodebase.md for multi-file, incremental adoption.
Run Claude Code as an MCP server so any agent can delegate coding tasks to it
Browser automation using accessibility snapshots instead of screenshots
Google's universal MCP server supporting PostgreSQL, MySQL, MongoDB, Redis, and 10+ databases
Official GitHub integration for repos, issues, PRs, and CI/CD workflows