"""MCP Resources for xcsift server.
Resources expose data that can be read by AI coding assistants.
"""
from mcp.server.fastmcp import FastMCP, Context
from mcp.server.session import ServerSession
from xcsift_mcp.xcsift_installer import get_xcsift_version, get_xcsift_path
# Example .xcsift.toml configuration template
CONFIG_TEMPLATE = '''# xcsift Configuration File
# Place this file as .xcsift.toml in your project root or ~/.config/xcsift/config.toml
# Output format: "json" (default), "toon", or "github-actions"
format = "json"
# Warning options
warnings = true # Print detailed warnings list (-w)
werror = false # Treat warnings as errors (-W)
# Output control
quiet = false # Suppress output on success (-q)
# Test analysis
slow_threshold = 1.0 # Threshold in seconds for slow test detection
# Coverage options
coverage = false # Enable coverage output (-c)
coverage_details = false # Include per-file coverage breakdown
coverage_path = "" # Custom path (empty = auto-detect)
# Build info
build_info = false # Include per-target phases and timing
executable = false # Include executable targets (-e)
# TOON format configuration
[toon]
delimiter = "comma" # "comma", "tab", or "pipe"
key_folding = "disabled" # "disabled" or "safe"
flatten_depth = 0 # 0 = unlimited
'''
def register_resources(mcp: FastMCP) -> None:
"""Register resources with the MCP server."""
@mcp.resource("xcsift://version")
async def get_version_resource(ctx: Context[ServerSession, None] | None = None) -> str:
"""Get xcsift version and installation information.
Returns version info about the installed xcsift binary.
"""
xcsift_path = get_xcsift_path()
if xcsift_path:
try:
version = await get_xcsift_version(xcsift_path)
return f"""xcsift Installation Info
========================
Path: {xcsift_path}
Version: {version}
Status: Installed and ready
"""
except Exception as e:
return f"""xcsift Installation Info
========================
Path: {xcsift_path}
Version: Error getting version: {e}
Status: Binary found but may not work
"""
else:
return """xcsift Installation Info
========================
Path: Not found
Version: N/A
Status: Not installed - will be downloaded on first use
"""
@mcp.resource("xcsift://config-template")
async def get_config_template() -> str:
"""Get an example .xcsift.toml configuration file.
Returns a template configuration file that can be used to
customize xcsift behavior for your project.
"""
return CONFIG_TEMPLATE
@mcp.resource("xcsift://output-formats")
async def get_output_formats_info() -> str:
"""Get information about available output formats.
Returns documentation about the JSON and TOON output formats.
"""
return '''xcsift Output Formats
=====================
## JSON Format (Default)
Standard JSON output with structured data:
```json
{
"status": "failed",
"summary": {
"errors": 1,
"warnings": 3,
"failed_tests": 0,
"linker_errors": 0
},
"errors": [
{
"file": "main.swift",
"line": 15,
"message": "use of undeclared identifier 'unknown'"
}
]
}
```
## TOON Format (Token-Optimized)
Token-Oriented Object Notation - 30-60% fewer tokens than JSON:
```
status: failed
summary:
errors: 1
warnings: 3
failed_tests: 0
linker_errors: 0
errors[1]{file,line,message}:
main.swift,15,"use of undeclared identifier 'unknown'"
```
### When to use each format:
- **JSON**: When you need to parse the output programmatically or
integrate with other tools.
- **TOON**: When sending to an LLM to reduce token usage and costs.
Best for analysis and debugging workflows.
### Token savings example:
Same build output (1 error, 3 warnings):
- JSON: 652 bytes
- TOON: 447 bytes
- Savings: 31.4%
'''
@mcp.resource("xcsift://help")
async def get_help() -> str:
"""Get help documentation for xcsift MCP server.
Returns comprehensive help about available tools and usage.
"""
return '''xcsift MCP Server Help
======================
## Overview
xcsift MCP server provides tools for parsing Xcode build output
into structured formats optimized for AI coding agents.
## Available Tools
### Parsing Tools
1. **parse_xcodebuild_output**
Parse raw xcodebuild/swift output into JSON or TOON format.
- Input: Raw build output (capture with 2>&1)
- Options: format, include_warnings, include_coverage
2. **extract_errors**
Extract only errors with file/line information.
3. **extract_warnings**
Extract only warnings with file/line/type information.
4. **extract_test_failures**
Extract test failures with assertion messages.
5. **get_build_summary**
Get a quick summary with error/warning counts.
### Build Execution Tools
1. **xcodebuild**
Run xcodebuild and parse output automatically.
- Supports: build, test, clean, analyze actions
- Options: scheme, project, workspace, destination, etc.
2. **swift_build**
Run swift build for SPM projects.
- Options: configuration, target, product
3. **swift_test**
Run swift test with optional coverage.
- Options: filter, coverage, parallel
4. **run_shell_build_command**
Run arbitrary build commands and parse output.
- Use for complex or custom build scenarios.
## Usage Examples
### Parse existing output:
```
parse_xcodebuild_output(output="<raw output>", format="toon")
```
### Run build and parse:
```
xcodebuild(action="build", scheme="MyApp", destination="platform=iOS Simulator,name=iPhone 15")
```
### Run tests with coverage:
```
swift_test(enable_code_coverage=True, output_format="json")
```
## Tips
1. Always capture stderr with stdout (2>&1) when piping to xcsift.
2. Use TOON format when you need to minimize tokens.
3. Enable include_warnings only when you need the details.
4. Use extract_* tools for focused analysis.
'''