ra-mcp
CLAUDE.md26 starsChanged 10 months agoApache-2.0View on GitHub
ra-mcp (Riksarkivet Model Context Protocol) is an MCP server that provides access to transcribed historical documents from the Swedish National Archives (Riksarkivet). The project is organized as a uv workspace with 45 packages under packages/ plus a root server. This includes ra-mcp-dataset-lib (module ra_mcp_dataset_lib) — the shared LanceDB "spine" (one SearchResult; get_lancedb cached/thread-safe connections; build_fts_index + build_scalar_indexes; an instrumented lancedb_fts_search; SQL predicate builders; and the shared MCP-handler scaffold require_keyword + format_results) that the 13 dataset libraries and the pdf-mcp guide…
- Sends data out
- Commits and pushes
In this repository4 other files it gives its agents.
- add-mcp-packageSkill
- releaseSkill
- zensical-authoringSkill
- zensical-setupSkill
The file
Show its rules and text
We split this CLAUDE.md into the rules it gives an agent
32 rules. About 0 are common, 16 are distinctive, and 16 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈16
Distinctive · estimate
This author's own choices. Worth a look.
16
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 172
Run the server using .docker/docker-compose.yml on the Dagger engine — no Docker daemon required.
- Distinctive: line 283
Never mock telemetry — test behavior, not instrumentation
- Distinctive: line 296
Test: behavior, return values, error handling, edge cases
- Distinctive: line 314
IMPORTANT: Before committing code, always run CI checks locally to catch issues early.
- Very specific: line 327
Best Practice: Run dagger call checks before every commit to ensure:
- Distinctive: line 590
Test-First Development: Never write code without testing it!
- Distinctive: line 591
Modify Over Create: Prefer editing existing files over creating new ones.
- Distinctive: line 592
Read Completely: Always read whole files, don't just read the head.
- Distinctive: line 593
Multiple Recommendations: When possible, provide 2-3 alternative solutions.
- Very specific: line 637
Create a new tool file in the appropriate MCP package (e.g., search_tool.py)
- Distinctive: line 639
Add detailed docstring with examples and parameter documentation
- Very specific: line 640
Call the register function from the package's tools.py
- Very specific: line 655
Create domain lib package: packages/libs/metadata-lib/ (module ra_mcp_metadata_lib) with models, client, operations
- Very specific: line 656
Create MCP package: packages/mcps/metadata-mcp/ (module ra_mcp_metadata_mcp) exposing a FastMCP server (e.g.
- Very specific: line 658
add ra-mcp-metadata-mcp to dependencies (always-on) or to [project.optional-dependencies] as its own extra (optional)
- Very specific: line 659
add ra-mcp-metadata-lib/ra-mcp-metadata-mcp under [tool.uv.sources] and known-first-party
- Very specific: line 705
Create a new client lib package (the ALTO/IIIF/OAI-PMH clients each live in their own *-lib package)
- Very specific: line 706
Follow existing patterns (see client.py for ALTOClient, or iiif-lib / oai-pmh-lib)
- Very specific: line 707
Use the centralized HTTPClient from ra_mcp_common
- Distinctive: line 708
Add comprehensive error handling
- Distinctive: line 709
Use dependency injection for HTTP client
- Very specific: line 853
All sub-packages use only opentelemetry-api (no-op when SDK absent)
- Very specific: line 896
Follow the HTTP client pattern (the gold standard in this codebase).
- Very specific: line 934
Check if port 7860 is already in use: lsof -i :7860
- Distinctive: line 937
Issue: No search results found
- Very specific: line 938
Verify API is accessible: curl https://data.riksarkivet.se/api/records
- Distinctive: line 939
Check search syntax (use exact terms first, then wildcards)
- Very specific: line 944
Check Python version: python --version (requires 3.13+)
- Distinctive: line 947
Test infrastructure is being set up - see Testing section
- Distinctive: line 967
When working with this codebase:
- Distinctive: line 969
Always test changes: Run the server or CLI to verify functionality
- Distinctive: line 970
Read full context: Use the Read tool on complete files, not just snippets
≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project Overview
ra-mcp (Riksarkivet Model Context Protocol) is an MCP server that provides access to transcribed historical documents from the Swedish National Archives (Riksarkivet).
Architecture
The project is organized as a uv workspace with 45 packages under packages/ plus a root server.
This includes ra-mcp-dataset-lib (module ra_mcp_dataset_lib) — the shared LanceDB "spine"
(one SearchResult; get_lancedb cached/thread-safe connections; build_fts_index +
build_scalar_indexes; an instrumented lancedb_fts_search; SQL predicate builders; and the
shared MCP-handler scaffold require_keyword + format_results) that the 13 dataset libraries
and the pdf-mcp guide search all reuse instead of each rolling their own search. Cross-cutting
MCP helpers live in ra-mcp-common: run_dev_server (the standalone dev entrypoint),
mark_span_error + record_span_exception (telemetry), and the HTTPClient.
Packages are grouped by role into subfolders:
packages/libs/— domain libraries (*-lib), pluscommonpackages/mcps/— FastMCP servers (*-mcp) that wrap a domain library as MCP tools/resourcespackages/cli/— CLI (search-cli,browse-cli) and terminal-UI (tui) front-ends
ra-mcp/
├── src/ra_mcp_server/ # Root: Server composition and main CLI
│ ├── server.py # FastMCP composition entry point + AVAILABLE_MODULES registry
│ └── cli/app.py # Typer CLI root (ra command)
├── packages/
│ ├── libs/ # domain libraries (*-lib) + common
│ │ ├── common/ # ra-mcp-common: Shared HTTP client, formatting, datasets, telemetry
│ │ ├── xml-lib/ # ra-mcp-xml: ALTOClient (ALTO XML parsing)
│ │ ├── iiif-lib/ # ra-mcp-iiif-lib: IIIFClient
│ │ ├── oai-pmh-lib/ # ra-mcp-oai-pmh-lib: OAIPMHClient
│ │ ├── search-lib/ # ra-mcp-search-lib: Search domain (models, client, operations)
│ │ ├── browse-lib/ # ra-mcp-browse-lib: Browse domain (models, operations, url_generator)
│ │ └── <dataset>-lib/ # 14 dataset libs (diplomatics, sbl, sjomanshus, filmcensur,
│ │ # rosenberg, court, aktiebolag, faltjagare, suffrage,
│ │ # specialsok, dds, wincars, sj, tora)
│ ├── mcps/ # FastMCP servers wrapping the libs (*-mcp)
│ │ ├── search-mcp/ # ra-mcp-search-mcp: MCP tools for search
│ │ ├── browse-mcp/ # ra-mcp-browse-mcp: MCP tool for browse
│ │ ├── guide-mcp/ # ra-mcp-guide-mcp: MCP resources for historical guides
│ │ ├── htr-mcp/ # ra-mcp-htr-mcp: Handwritten text recognition (HTRflow)
│ │ ├── viewer-mcp/ # ra-mcp-viewer-mcp: Interactive document viewer (MCP App UI)
│ │ ├── pdf-mcp/ # ra-mcp-pdf-mcp: Interactive PDF viewer (MCP App UI)
│ │ ├── label-mcp/ # ra-mcp-label-mcp: Label Studio import (optional)
│ │ └── <dataset>-mcp/ # 14 dataset MCP servers (one per dataset lib above)
│ └── cli/ # CLI + TUI front-ends
│ ├── search-cli/ # ra-mcp-search-cli: CLI command for search
│ ├── browse-cli/ # ra-mcp-browse-cli: CLI command for browse
│ └── tui/ # ra-mcp-tui: Interactive terminal browser
├── resources/ # Historical guide markdown files
├── pyproject.toml # Workspace configuration
└── uv.lock # Shared lockfilePackage Structure
ra-mcp-common (no internal dependencies):
- http_client.py: Centralized httpx-based async HTTP client with logging
- formatting.py, datasets.py, telemetry.py
Shared client libs (depend on common): the ALTO/IIIF/OAI-PMH clients were extracted out of browse into their own packages so any package can reuse them:
- ra-mcp-xml:
ALTOClient(ALTO XML fetching + parsing) - ra-mcp-iiif-lib:
IIIFClient - ra-mcp-oai-pmh-lib:
OAIPMHClient
ra-mcp-search-lib (module ra_mcp_search_lib, depends on common):
- config.py: Search API URL and constants
- models.py: Pydantic models (SearchRecord, RecordsResponse, SearchResult)
- search_client.py: SearchClient client
- search_operations.py: Search business logic
ra-mcp-browse-lib (module ra_mcp_browse_lib, depends on common + xml/iiif/oai-pmh libs):
- config.py: Browse API URLs and constants
- models.py: Pydantic models (BrowseResult, PageContext)
- browse_operations.py: Browse business logic (uses ALTOClient, IIIFClient, OAIPMHClient)
- url_generator.py: URL construction helpers
ra-mcp-search-mcp (depends on search-lib + fastmcp):
- tools.py: FastMCP server setup, instructions, and tool registration
- search_tool.py:
search_transcribedandsearch_metadataMCP tools - server.py: Standalone entry point for isolated dev/testing
- formatter.py: Search result formatting for LLM output
ra-mcp-browse-mcp (depends on browse-lib + fastmcp):
- tools.py: FastMCP server setup, instructions, and tool registration
- browse_tool.py:
browse_documentMCP tool - server.py: Standalone entry point for isolated dev/testing
- formatter.py: Browse result formatting for LLM output
ra-mcp-search-cli (module ra_mcp_search_cli, depends on search-lib + typer + rich):
- app.py: Typer sub-app (
search_app) - search_cmd.py:
ra searchCLI command - formatter.py: CLI output formatting
ra-mcp-browse-cli (module ra_mcp_browse_cli, depends on browse-lib + typer + rich):
- app.py: Typer sub-app (
browse_app) - browse_cmd.py:
ra browseCLI command - formatter.py: CLI output formatting
ra-mcp-tui (module ra_mcp_tui): Textual-based interactive terminal browser (tui_app, ra tui).
ra-mcp-guide-mcp (depends on common + fastmcp):
- tools.py: FastMCP server and MCP resources for historical guides from
resources/ - server.py: Standalone entry point for isolated dev/testing
Dataset / feature MCP modules: htr-mcp, viewer-mcp, pdf-mcp, label-mcp, and 13 LanceDB-backed dataset modules (each a <name>-lib + <name>-mcp pair): diplomatics, sbl, sjomanshus, filmcensur, rosenberg, court, aktiebolag, faltjagare, suffrage, specialsok, dds, wincars, sj, plus tora geocoding. These follow the same lib/mcp split.
Root package — ra-mcp (depends on all always-on MCP packages; dataset + CLI packages are optional extras):
- server.py: FastMCP composition server with the
AVAILABLE_MODULESregistry (imports the always-on modules eagerly, optional modules via guardedtry/import) - cli/app.py: Main Typer CLI entry point (
racommand). Note it importssearch_app,browse_app, andtui_appunconditionally, so theracommand requires thecliandtuiextras to be installed.
Package Dependencies
ra-mcp-common (no internal deps)
↑
ra-mcp-xml / iiif-lib / oai-pmh-lib (depend on common)
ra-mcp-search-lib (depends on common)
ra-mcp-browse-lib (depends on common + xml/iiif/oai-pmh libs)
<dataset>-lib (depend on common)
↑
ra-mcp-search-mcp (depends on search-lib + fastmcp)
ra-mcp-browse-mcp (depends on browse-lib + fastmcp)
ra-mcp-guide-mcp / htr-mcp / viewer-mcp … (depend on common/<dataset>-lib + fastmcp)
<dataset>-mcp (depend on <dataset>-lib + fastmcp)
ra-mcp-search-cli (depends on search-lib + typer + rich)
ra-mcp-browse-cli (depends on browse-lib + typer + rich)
ra-mcp-tui (depends on search-lib/browse-lib + textual)
↑
ra-mcp (root) (composes the MCP modules; CLI/TUI/dataset packages are optional extras)Development Workflow
Setup
# Clone repository
git clone https://github.com/AI-Riksarkivet/ra-mcp.git
cd ra-mcp
# Install dependencies (syncs workspace packages)
uv sync
# Build the viewer-mcp and pdf-mcp App UIs (npm build of both UIs).
# Required before `ra serve`, or the MCP App UIs won't be built.
make build-uiThe make serve / make serve-http targets run build-ui automatically. If you launch the
server directly with uv run ra serve, run make build-ui yourself first.
Running the Server
# MCP server (stdio) - for Claude Desktop integration
uv run ra serve
# MCP server (HTTP/SSE) - for web clients, testing, and development
uv run ra serve --port 7860
# With verbose logging
uv run ra serve --port 7860 --logDocker Compose (via Dagger)
Run the server using .docker/docker-compose.yml on the Dagger engine — no Docker daemon required.
Configuration mirrors the Helm chart (charts/ra-mcp/values.yaml).
# Start server (exposed on host port 7860)
dagger call compose-up up --ports 7860:7860
# or
make compose-up
# Run health check
dagger call compose-test
# or
make compose-testConnect to Claude Code:
# 1. Start the server (keep this terminal running)
dagger call compose-up up --ports 7860:7860
# 2. In another terminal, add as MCP server
claude mcp add --transport http ra-mcp http://localhost:7860/mcp
# 3. Verify connection inside Claude Code
/mcp
# 4. Test with a search
# Ask Claude: "search for trolldom"Verify manually:
# Health check
curl http://localhost:7860/health
# MCP endpoint (should return server info)
curl -X POST http://localhost:7860/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"test","version":"0.1"}}}'Using the CLI
The project includes a full-featured CLI for searching and browsing documents:
# Search for documents
uv run ra search "trolldom"
uv run ra search "Stockholm" --max 50
# Browse specific documents
uv run ra browse "SE/RA/310187/1" --page "7,8,52"
uv run ra browse "SE/RA/420422/01" --pages "1-10" --search-term "Stockholm"
# Get help
uv run ra --help
uv run ra search --help
uv run ra browse --helpTesting
# Run all tests
uv run pytest
# Run specific package tests
uv run pytest packages/libs/common/tests/ -v
uv run pytest packages/libs/search-lib/tests/ -v
uv run pytest packages/libs/browse-lib/tests/ -v
# Run with coverage
uv run pytest --cov=ra_mcp_common --cov=ra_mcp_search_lib --cov=ra_mcp_browse_lib --cov-report=htmlTesting Principles
Tests follow patterns drawn from httpx, pydantic, and FastMCP. Build bottom-up through the dependency stack:
Layer 0: ra-mcp-common ← pure utilities, no internal deps
Layer 1: *-lib packages ← domain models, clients, operations
(xml, iiif-lib, oai-pmh-lib, search-lib, browse-lib, <dataset>-lib)
Layer 2: *-mcp, *-cli, tui packages ← MCP tools, CLI commands, terminal UI
Layer 3: ra-mcp root server ← compositionStructure:
- One test file per source module:
test_formatting.pytestsformatting.py - Flat module-level functions — no test classes (httpx pattern)
- Each test file is self-contained with its own helpers and mock data
- Fixtures in
conftest.pyonly for truly shared setup (e.g., mock HTTP response factory) - XML/JSON fixture files in
packages/<pkg>/tests/fixtures/
Parametrize for edge cases (pydantic pattern):
@pytest.mark.parametrize("page_id,expected", [
pytest.param("_00066", 66, id="standard"),
pytest.param("_H0000459_00005", 5, id="compound"),
pytest.param("_00000", 0, id="all-zeros"),
])
def test_page_id_to_number(page_id, expected):
assert page_id_to_number(page_id) == expectedMock at the right boundary:
- Domain libs (search-lib, browse-lib, the client libs): inject a mock
HTTPClientvia constructor — don't patchhttpx - MCP tool packages: use
Client(mcp)for in-process testing, mock at the operations layer - Never mock telemetry — test behavior, not instrumentation
MCP tool testing (FastMCP pattern):
async def test_tool_returns_error_on_empty_keyword():
async with Client(search_mcp) as client:
result = await client.call_tool("transcribed", {"keyword": "", "offset": 0})
assert "empty" in result.content[0].text.lower()Naming: test_<subject>_<scenario> — e.g., test_get_json_success, test_get_content_returns_none_on_404
What to test vs skip:
- Test: behavior, return values, error handling, edge cases
- Skip: telemetry span attributes, log messages,
__init__.pyre-exports, config constants
Code Quality
# Format code
uv run ruff format .
# Lint and auto-fix issues
uv run ruff check --fix .
# Type check (ty is configured in pyproject.toml)
uv run ty checkRunning CI Checks Locally
IMPORTANT: Before committing code, always run CI checks locally to catch issues early.
# Run full CI pipeline (same as GitHub Actions)
dagger call checks && dagger call test
# Quick check: Run code quality checks only
dagger call checks
# Quick check: Run tests only
dagger call testBest Practice: Run dagger call checks before every commit to ensure:
- Code is properly formatted
- No linting errors
- Type checking passes
- Tests pass (when implemented)
This matches exactly what runs in GitHub Actions CI, preventing failed builds.
Debugging
# Test MCP server with MCP Inspector
npx @modelcontextprotocol/inspector uv run ra serve
# Test HTTP/SSE server with curl
uv run ra serve --port 7860
curl http://localhost:7860/mcpTesting with Dagger
You can build and test the containerized server using Dagger:
# Build and test server with automatic health check
dagger call test-server --source=.
# Just build the container
dagger call build --source=.
# Start server as a Dagger service (for testing with other containers)
dagger call serve --source=. --port=7860
# Expose server on host for manual testing
dagger call serve-up --source=. --port=7860 upInteractive testing with Dagger shell:
# Build and get a shell in the container
dagger call build --source=. terminal
# Inside the container, you can:
# - Test the server: ra serve --host 0.0.0.0 --port 7860
# - Run CLI commands: ra search "trolldom"
# - Check environment: python --version
# - Debug issues: ls -la /appBuilding and Publishing
Prerequisites
The project uses Dagger for containerized builds and publishing.
For Docker publishing:
export DOCKER_USERNAME="your-dockerhub-username"
export DOCKER_PASSWORD="your-dockerhub-token-or-password"For PyPI publishing:
export PYPI_TOKEN="your-pypi-token"Build Commands
# Build container image locally (default: Alpine)
dagger call build --source=.
# Build with specific base image
dagger call build --source=. --base-image="python:3.13-alpine"
dagger call build --source=. --base-image="cgr.dev/chainguard/python:latest-dev"
dagger call build --source=. --base-image="python:3.13-slim"
# Run tests (currently skipped until test suite exists)
dagger call test --source=.
# Build with custom settings
dagger call build-local \
--source=. \
--image-repository="riksarkivet/ra-mcp" \
--base-image="python:3.13-alpine"Security: SBOM and Attestations
The project supports generating Software Bill of Materials (SBOM) and security scanning:
Generate SBOM (Software Bill of Materials):
# Generate SBOM in SPDX format (default)
dagger call generate-sbom-spdx --source=. --base-image="python:3.13-alpine"
# Generate SBOM in CycloneDX format
dagger call generate-sbom-cyclone-dx --source=. --base-image="python:3.13-alpine"
# Export SBOM to local file
dagger call export-sbom \
--source=. \
--base-image="python:3.13-alpine" \
--format="spdx-json" \
--output-path="./sbom.spdx.json"Vulnerability Scanning:
# Scan for vulnerabilities (CRITICAL and HIGH)
dagger call scan-ci --source=.
# Scan with custom severity levels
dagger call scan --source=. --severity="CRITICAL,HIGH,MEDIUM" --format="table"
# Generate JSON scan report
dagger call scan-json --source=. --severity="CRITICAL,HIGH"
# Generate SARIF output for GitHub Security
dagger call scan-sarif --source=. --output-path="trivy-results.sarif"Important Notes:
- SBOM Generation: Uses Trivy to scan the built container and generate SPDX or CycloneDX SBOMs
- Provenance: SLSA provenance is generated by BuildKit during the GitHub Actions publish workflow
- GitHub Releases: SBOMs and provenance are automatically generated and attached to releases as assets
- Verification: SBOMs can be used to verify container contents and detect supply chain issues
- Format Support: SPDX-JSON and CycloneDX-JSON formats are both supported
Publishing to Docker Registry
The project supports multiple base images for different use cases:
Supported Base Images:
python:3.13-alpine- Lightweight Alpine Linux (default)cgr.dev/chainguard/python:latest-dev- Wolfi-based Chainguard image (minimal CVEs)cgr.dev/chainguard/python:latest- Chainguard production imagepython:3.13-slim- Debian slim variant- Any Python 3.13+ image with pip support
Publishing Examples:
# Publish Alpine variant with explicit tag
dagger call publish-docker \
--docker-username=env:DOCKER_USERNAME \
--docker-password=env:DOCKER_PASSWORD \
--image-repository="riksarkivet/ra-mcp" \
--tag="v0.14.2" \
--base-image="python:3.13-alpine" \
--tag-suffix="-alpine" \
--source=.
# Result: riksarkivet/ra-mcp:v0.14.2-alpine
# Publish Wolfi/Chainguard variant
dagger call publish-docker \
--docker-username=env:DOCKER_USERNAME \
--docker-password=env:DOCKER_PASSWORD \
--image-repository="riksarkivet/ra-mcp" \
--tag="v0.14.2" \
--base-image="cgr.dev/chainguard/python:latest-dev" \
--tag-suffix="-wolfi" \
--source=.
# Result: riksarkivet/ra-mcp:v0.14.2-wolfi
# Publish Debian slim variant
dagger call publish-docker \
--docker-username=env:DOCKER_USERNAME \
--docker-password=env:DOCKER_PASSWORD \
--image-repository="riksarkivet/ra-mcp" \
--tag="v0.14.2" \
--base-image="python:3.13-slim" \
--tag-suffix="-slim" \
--source=.
# Result: riksarkivet/ra-mcp:v0.14.2-slim
# Auto-tag from pyproject.toml version (prefixes with "v")
dagger call publish-docker \
--docker-username=env:DOCKER_USERNAME \
--docker-password=env:DOCKER_PASSWORD \
--image-repository="riksarkivet/ra-mcp" \
--base-image="python:3.13-alpine" \
--tag-suffix="-alpine" \
--source=.GitHub Actions Publishing:
The publishing workflow (publish.yml) uses docker/build-push-action for native BuildKit attestation support:
- Publishes container images with embedded attestations:
- Alpine:
riksarkivet/ra-mcp:v0.14.2-alpine - Wolfi:
riksarkivet/ra-mcp:v0.14.2-wolfi - SBOM attestations embedded in registry manifest
- SLSA Provenance (mode=max) embedded in registry
- Alpine:
- Also generates standalone SBOM files:
sbom-v0.14.2-alpine.spdx.json(as release asset)sbom-v0.14.2-wolfi.spdx.json(as release asset)
Verify registry attestations:
# Inspect SBOM in registry
docker buildx imagetools inspect riksarkivet/ra-mcp:v0.14.2-alpine --format "{{json .SBOM}}"
# Inspect provenance
docker buildx imagetools inspect riksarkivet/ra-mcp:v0.14.2-alpine --format "{{json .Provenance}}"
# Verify with Docker Scout
docker scout attestation riksarkivet/ra-mcp:v0.14.2-alpineSecurity Benefits:
- Registry-native attestations (embedded in image manifest)
- SLSA Provenance Level 3 (build process transparency)
- SBOM attestations (dependency transparency)
- Multi-platform builds (amd64, arm64)
- Compliance ready (NTIA, EO 14028, SLSA)
Publishing to PyPI
# Build and publish to PyPI
dagger call publish-pypi \
--pypi-token=env:PYPI_TOKEN \
--source=.Claude Code Integration
Add MCP Server to Claude Code
# HTTP/SSE transport (recommended for development)
claude mcp add --transport sse ra-mcp http://localhost:7860/sse
# Verify connection
claude mcp listClaude Desktop Integration
Add to claude_desktop_config.json:
macOS/Linux: ~/.config/claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"ra-mcp": {
"command": "uv",
"args": ["run", "ra", "serve"],
"env": {}
}
}
}Coding Guidelines
Core Principles
- Test-First Development: Never write code without testing it! Always verify your changes work.
- Modify Over Create: Prefer editing existing files over creating new ones.
- Read Completely: Always read whole files, don't just read the head.
- Multiple Recommendations: When possible, provide 2-3 alternative solutions.
Documentation Standards
When to use docstrings:
- Public APIs and MCP tools
- Complex business logic
- Non-obvious behavior or algorithms
- Functions with important pre/post-conditions
When to use better naming instead:
- Simple, obvious functions
- Well-known patterns
- Standard operations
API Endpoints
Live HTTP APIs (Riksarkivet)
The search/browse/viewer modules call these remote services directly:
- Search API:
https://data.riksarkivet.se/api/records- Documentation - IIIF Collections:
https://lbiiif.riksarkivet.se/collection/arkiv- Documentation - IIIF Images:
https://lbiiif.riksarkivet.se - ALTO XML:
https://sok.riksarkivet.se/dokument/alto - Bildvisning:
https://sok.riksarkivet.se/bildvisning(Interactive viewer) - OAI-PMH:
https://oai-pmh.riksarkivet.se/OAI- Documentation
Local LanceDB dataset modules
In addition to the live HTTP APIs above, the server registers optional dataset modules that query
local LanceDB indexes (no remote API) rather than the Riksarkivet HTTP endpoints: diplomatics,
sbl, sjomanshus, filmcensur, rosenberg, court, aktiebolag, faltjagare, suffrage, specialsok, dds,
wincars, sj. Each ships as a <name>-lib (data access) + <name>-mcp (tools) pair and is registered
via a guarded try/import in AVAILABLE_MODULES (see server.py), so a
missing lancedb wheel just skips the module instead of breaking startup.
Additional Resources
- Riksarkivet Data Platform Wiki: Comprehensive API documentation
- Förvaltningshistorik: Semantic search interface (experimental)
- HTRflow: Handwritten text recognition pipeline (PyPI package)
Common Tasks
Adding a New MCP Tool
- Create a new tool file in the appropriate MCP package (e.g., search_tool.py)
- Define a
register_*_tool(mcp)function that uses@mcp.tool()decorator - Add detailed docstring with examples and parameter documentation
- Call the register function from the package's tools.py
Example pattern (from search_tool.py):
def register_search_tool(mcp: FastMCP):
@mcp.tool()
async def search_transcribed(keyword: str, offset: int, ...) -> str:
"""Tool description for LLM understanding."""
...Adding a New MCP Module
To add a new module (e.g., ra-mcp-metadata):
- Create domain lib package:
packages/libs/metadata-lib/(modulera_mcp_metadata_lib) with models, client, operations - Create MCP package:
packages/mcps/metadata-mcp/(modulera_mcp_metadata_mcp) exposing a FastMCP server (e.g.metadata_mcp) - Register the workspace member + source mapping in the root pyproject.toml:
- add
ra-mcp-metadata-mcptodependencies(always-on) or to[project.optional-dependencies]as its own extra (optional) - add
ra-mcp-metadata-lib/ra-mcp-metadata-mcpunder[tool.uv.sources]andknown-first-party
- add
Register in server.py
AVAILABLE_MODULES.Always-on modules (search, browse, guide, htr, viewer, pdf) are imported eagerly at the top of the file and added to the
AVAILABLE_MODULESdict literal:from ra_mcp_metadata_mcp import metadata_mcp AVAILABLE_MODULES = { ... "metadata": { "server": metadata_mcp, "description": "Advanced metadata search and filtering", "default": True, }, }Optional modules (label + the LanceDB datasets) follow the guarded
try/importpattern so a missing optional dependency just skips the module instead of breaking startup:try: from ra_mcp_metadata_mcp import metadata_mcp # ty: ignore[unresolved-import] AVAILABLE_MODULES["metadata"] = { "server": metadata_mcp, "description": "Advanced metadata search and filtering", "default": True, } except ImportError: passOptional notes: set
"no_namespace": Trueto mount the module's tools without a namespace prefix (as viewer/pdf/sbl do).- Optionally add a CLI/TUI front-end and wire it into the relevant extra.
The server currently registers 21 modules: 6 always-on (search, browse, guide, htr, viewer, pdf) plus 15 optional (label, diplomatics, sbl, sjomanshus, filmcensur, rosenberg, court, aktiebolag, faltjagare, suffrage, specialsok, dds, wincars, sj, tora).
Adding API Clients
- Create a new client lib package (the ALTO/IIIF/OAI-PMH clients each live in their own
*-libpackage) - Follow existing patterns (see client.py for
ALTOClient, or iiif-lib / oai-pmh-lib) - Use the centralized HTTPClient from
ra_mcp_common - Add comprehensive error handling
- Use dependency injection for HTTP client
Adding a New MCP Resource
Resources provide static or dynamic content to MCP clients (see guide_mcp/tools.py for examples):
@mcp.resource("riksarkivet://my-resource/{param}")
def get_my_resource(param: str) -> str:
"""Description of what this resource provides."""
return f"Content for {param}"Updating Dependencies
# Add new dependency to a package
cd packages/libs/common && uv add package-name
# Add development dependency (root)
uv add --dev package-name
# Update all dependencies
uv sync --upgrade
# Update specific dependency
uv add package-name@latestWorking with Git
# Check status
git status
# Stage changes
git add .
# Commit with conventional commit format
git commit -m "feat: add new search feature"
git commit -m "fix: resolve timeout issue"
git commit -m "docs: update API documentation"
# Push changes
git pushConventional Commit Types:
feat: New featurefix: Bug fixdocs: Documentation changesrefactor: Code refactoringtest: Adding testschore: Maintenance tasksperf: Performance improvements
Releasing
Releases are automated via GitHub Actions. The workflow chain is:
git tag v0.X.Y && git push --tags
→ release.yml: generates changelog with git-cliff, creates GitHub Release
→ publish.yml: triggers on release:published, builds & pushes Docker imagesTo cut a new release:
# One command: bumps pyproject.toml, commits, tags, and pushes
make release VERSION=0.5.0This runs: sed version update → git commit → git tag v0.5.0 → git push && git push --tags.
How it works:
- release.yml triggers on
v*tag pushes - Installs
git-cliffand runsgit cliff --latest --strip headerto generate notes for the tagged version - Creates a GitHub Release with those notes via
softprops/action-gh-release - Release notes include commit links back to GitHub (configured in cliff.toml)
- The existing publish.yml triggers on
release: [published]to build and push Docker images
Local changelog generation:
# Full changelog to CHANGELOG.md
make changelog
# Preview release notes for the latest tag
uvx git-cliff --latest --strip headerDebugging Tips
Environment Variables for Debugging:
# Set logging level (DEBUG, INFO, WARNING, ERROR)
export RA_MCP_LOG_LEVEL=DEBUG
# Enable API call logging to file (ra_mcp_api.log)
export RA_MCP_LOG_API=1
# Override timeout (useful for Hugging Face)
export RA_MCP_TIMEOUT=120MCP Server Issues:
# Use MCP Inspector for interactive testing
npx @modelcontextprotocol/inspector uv run ra serve
# Enable verbose logging with environment variable
RA_MCP_LOG_LEVEL=DEBUG uv run ra serve --port 7860
# Test HTTP endpoint
curl -X POST http://localhost:7860/mcp \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'Search Issues:
# Test search directly via CLI with debug logging
RA_MCP_LOG_LEVEL=DEBUG uv run ra search "test query" --max 5
# Check API response
curl "https://data.riksarkivet.se/api/records?q=Stockholm&rows=1"
# Monitor API logs (if RA_MCP_LOG_API=1)
tail -f ra_mcp_api.logObservability & Telemetry
Architecture
The project has a two-layer OpenTelemetry instrumentation strategy:
- FastMCP built-in instrumentation (automatic): FastMCP 3.0+ has native OTel support. It automatically creates spans for all
tools/call,resources/read,prompts/get, anddelegateoperations with MCP semantic convention attributes. Do NOT add manual spans to MCP tool handlers — FastMCP already covers them. - Manual instrumentation (project code): Domain operations, API clients, and the HTTP client have manual spans and metrics via
ra_mcp_common.telemetry.get_tracer().
How it works
- OTel SDK is initialized only in the root package (
src/ra_mcp_server/telemetry.py), gated onRA_MCP_OTEL_ENABLED=true - All sub-packages use only
opentelemetry-api(no-op when SDK absent) - FastMCP uses the same global
TracerProvider, so its automatic spans and project manual spans form a unified trace tree - Module-level tracers:
get_tracer("ra_mcp.<component>")fromra_mcp_common.telemetry
Trace tree (MCP tool call)
tools/call search_transcribed ← FastMCP (automatic)
└── delegate search_transcribed ← FastMCP (composed server)
└── tools/call search_transcribed ← FastMCP (local provider)
└── SearchOperations.search ← manual span
└── SearchClient.search ← manual span
└── HTTP GET ← manual spanEnvironment variables
RA_MCP_OTEL_ENABLED=true # Master switch (default: false)
OTEL_EXPORTER_OTLP_ENDPOINT=... # Collector endpoint (default: http://localhost:4317)
OTEL_EXPORTER_OTLP_PROTOCOL=grpc # grpc or http/protobuf (default: grpc)
OTEL_SERVICE_NAME=ra-mcp # Service name (default: ra-mcp)
ENVIRONMENT=production # deployment.environment.name resource attr (optional)
RA_MCP_OTEL_LOG_BRIDGE=true # Bridge Python logging to OTel (default: true)What's instrumented manually
| Component | Tracer name | Spans | Metrics |
| HTTP client | ra_mcp.http_client | HTTP GET | request count, error count, retry count, duration, response size |
| Search client | ra_mcp.search.client | SearchClient.search | — |
| Search ops | ra_mcp.search_operations | SearchOperations.search | request count (ra_mcp.search.requests), results per search (ra_mcp.search.results) |
| Browse ops | ra_mcp.browse_operations | BrowseOperations.browse_document, BrowseOperations._fetch_page_contexts | request count (ra_mcp.browse.requests), pages per browse (ra_mcp.browse.pages), empty pages (ra_mcp.browse.empty_pages) |
| ALTO client | ra_mcp.alto_client | ALTOClient.fetch_content | fetch count (ra_mcp.alto.fetches) |
| IIIF client | ra_mcp.iiif_client | IIIFClient.get_collection, IIIFClient.fetch_manifest | — |
| OAI-PMH client | ra_mcp.oai_pmh_client | OAIPMHClient.get_metadata, OAIPMHClient.extract_manifest_id | fetch count (ra_mcp.oai_pmh.fetches) |
| Viewer fetchers | fastmcp tracer (fetchers.py) | fetch_text_layer | — |
| Search CLI | ra_mcp.cli.search | cli.search | — |
| Browse CLI | ra_mcp.cli.browse | cli.browse | — |
Error recording pattern
Follow the HTTP client pattern (the gold standard in this codebase). The OTel
Span Event API (span.record_exception) is being deprecated, so exception
detail is emitted as a structured log record via the shared
record_span_exception(logger, exc) helper (ra_mcp_common.telemetry) instead
of a span event. The span still carries set_status(ERROR, ...) — with the
error class in the message — and the log record (exception.type /
exception.message / exception.stacktrace) inherits the active trace_id /
span_id through the OTLP log bridge:
from ra_mcp_common.telemetry import record_span_exception
except SomeError as e:
span.set_status(StatusCode.ERROR, f"{type(e).__name__}: {e}")
record_span_exception(logger, e)
self._error_counter.add(1, {"error.type": type(e).__name__})
raiseVerify telemetry
# Run full telemetry verification (starts Jaeger, exercises CLI, checks trace tree)
dagger call verify-telemetry --source=.MCP Specification Reference
For detailed information about the Model Context Protocol specification, implementation details, or when clarification is needed about MCP-specific features, refer to the official documentation:
- MCP Specification: Official protocol specification
- FastMCP Documentation: FastMCP library documentation
Troubleshooting
Common Issues
Issue: Server won't start
- Check if port 7860 is already in use:
lsof -i :7860 - Try a different port:
uv run ra serve --port 8001
Issue: No search results found
- Verify API is accessible:
curl https://data.riksarkivet.se/api/records - Check search syntax (use exact terms first, then wildcards)
- Try broader search terms
Issue: Import errors
- Reinstall dependencies:
uv sync --reinstall - Check Python version:
python --version(requires 3.13+)
Issue: Tests not running
- Test infrastructure is being set up - see Testing section
- Dagger currently accepts zero tests as passing
Getting Help
# General help
uv run ra --help
# Command-specific help
uv run ra serve --help
uv run ra search --help
uv run ra browse --help
# Check version
uv run ra --versionNotes for Claude Code
When working with this codebase:
- Always test changes: Run the server or CLI to verify functionality
- Read full context: Use the Read tool on complete files, not just snippets
- Prefer modifications: Edit existing code rather than creating new files
- Check types: The project uses type hints - maintain them in all code
- Follow patterns: Match existing code style and patterns (see packages/libs/search-lib/src/ra_mcp_search_lib/)
- Document thoroughly: MCP tools need excellent documentation for LLM understanding
- Workspace awareness: Changes to common affect all packages; changes to search-lib affect search-mcp, search-cli, and the TUI
- Layered architecture: Domain logic lives in
*-libpackages; MCP wrappers in*-mcppackages; CLI in*-clipackages and the TUI intui
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

