SwiftLens is a Model Context Protocol (MCP) server that provides deep, semantic-level analysis of Swift codebases to AI agents. By integrating directly with Apple's SourceKit-LSP, SwiftLens enables AI models to understand Swift code with compiler-grade accuracy.
SwiftLens bridges AI models and Swift development through:
AI Agent (Claude/GPT) → MCP Protocol → SwiftLens → SourceKit-LSP → Swift Code
- LSP-Powered Semantic Analysis: Leverages SourceKit-LSP for Xcode-grade accuracy
- Token-Optimized Output: Minimizes token usage for AI interactions
- Real-time Code Understanding: Analyzes Swift files with full language feature support
- Cross-file Navigation: Symbol references, definitions, and project-wide analysis
- Code Modification Tools: Safe, atomic file operations for AI-driven refactoring
- Zero Configuration: Works out-of-the-box with standard Swift projects
- Modern Swift syntax (actors, async/await, property wrappers, result builders)
- Generic types and protocols
- Swift Package Manager and Xcode projects
- Unicode identifiers and symbols
- Complex nested types and extensions
- macOS (required for SourceKit-LSP)
- Python 3.10+
- Xcode (full installation from App Store, not just Command Line Tools)
Add to your json configuration file mcpServers section:
{
"mcpServers": {
"swiftlens": {
"command": "uvx",
"args": ["swiftlens"]
}
}
}
SwiftLens will need proper sourcekit-lsp index in order to work properly you can either
"hey claude, run swift_build_index tool"
SwiftLens requires an index store for cross-file analysis. Build it with:
# Navigate to your Swift project
cd /path/to/your/swift/project
# Build with index store
swift build -Xswiftc -index-store-path -Xswiftc .build/index/store
Important: Rebuilding of the index is required when you:
- Add new Swift files
- Change public interfaces
- Notice missing symbol references
SwiftLens provides 15 tools for Swift code analysis:
swift_analyze_file
- Analyze structure and symbols in a Swift fileswift_analyze_multiple_files
- Batch analyze multiple filesswift_summarize_file
- Get symbol counts and file summaryswift_get_symbols_overview
- Extract top-level type declarationsswift_get_declaration_context
- Get fully-qualified symbol pathsswift_get_file_imports
- Extract import statementsswift_validate_file
- Validate syntax and types with swiftcswift_check_environment
- Verify Swift development setupswift_build_index
- Build index store db of current project for sourcekit-lsp
swift_find_symbol_references
- Find all references to a symbolswift_get_symbol_definition
- Jump to symbol definitionswift_get_hover_info
- Get type info and documentation
swift_replace_symbol_body
- Replace function/type body
swift_search_pattern
- Search with regex patternsget_tool_help
- Get help for any tool
Ask your AI agent:
"Analyze the UserManager.swift file and find all references to the login() method"
The AI will use SwiftLens tools to:
- Analyze the file structure
- Locate the login method
- Find all project-wide references
- Provide insights based on the analysis
SwiftLens includes a web dashboard for monitoring AI interactions:
- URL: http://localhost:53729 (when server is running)
- Features: Live tool execution logs, usage analytics, session tracking
- Security: Localhost-only access, no external connections
Rebuild your index:
swift build -Xswiftc -index-store-path -Xswiftc .build/index/store
New files need to be indexed:
swift build -Xswiftc -index-store-path -Xswiftc .build/index/store
Ensure Xcode is properly installed:
xcode-select -p # Should show Xcode path
xcrun sourcekit-lsp --help # Should show help text
# All tests
make test
# Unit tests only (fast, no LSP required)
make test-unit
# LSP integration tests
make test-lsp
# Check environment
make check-env
# Format code
./format.sh
# Check formatting
./format.sh check
SwiftLens is licensed under the Elastic License 2.0:
- ✅ Free for personal use and evaluation
⚠️ Commercial use requires a license
See LICENSE.md for details.
We welcome contributions! Areas where you can help:
- 🐛 Bug reports and fixes
- 📝 Documentation improvements
- 🧪 Test coverage expansion
- 🔧 New analysis capabilities
- Issues: GitHub Issues
SwiftLens - Bringing compiler-grade Swift understanding to AI development