{"data":{"slug":"github-xberg-io-xberg-ai-rulez-skills-api-server-mcp-dd4cf78158a0","title":"API Server & MCP Protocol Integration","summary":"Configure and manage the API server and MCP protocol for document extraction and model context processing.","url":"https:\/\/skillshunter.net\/skills\/github-xberg-io-xberg-ai-rulez-skills-api-server-mcp-dd4cf78158a0","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-xberg-io-xberg-ai-rulez-skills-api-server-mcp-dd4cf78158a0","markdown_url":"https:\/\/skillshunter.net\/skills\/github-xberg-io-xberg-ai-rulez-skills-api-server-mcp-dd4cf78158a0.md","download_url":"https:\/\/skillshunter.net\/skills\/github-xberg-io-xberg-ai-rulez-skills-api-server-mcp-dd4cf78158a0\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"The API server uses Axum and supports rmcp for model interactions. Ensure the environment variables like XBERG_HOST and XBERG_PORT are set before starting the server.","instructions":"---\ntype: Playbook\ntitle: Api Server Mcp\ndescription: \"Change REST routes, middleware, cache endpoints, server startup or MCP tools.\"\npaths:\n  - \"crates\/xberg\/src\/api\/**\"\n  - \"crates\/xberg\/src\/mcp\/**\"\n  - \"crates\/xberg\/src\/cache\/**\"\nx-ai-rulez:\n  kind: skill\n  id: api-server-mcp\n  metadata:\n    priority: critical\n    name: api-server-mcp\n---\n\n# API Server & MCP Protocol\n\n**Axum server for document extraction, plus the rmcp Model Context Protocol surface**\n\nLocations: `crates\/xberg\/src\/api\/` (`router.rs`, `handlers.rs`, `startup.rs`, `types.rs`,\n`error.rs`, `jobs.rs`) and `crates\/xberg\/src\/mcp\/`. There is no `api\/server.rs`.\n\n## REST routes\n\nRegistered in `api\/router.rs`, all on one `Router`:\n\n| Route | Handler |\n| --- | --- |\n| `POST \/extract` | `extract_handler` \u2014 multipart files, URL fields, or JSON; builds `ExtractInput` |\n| `POST \/extract-async` | `extract_async_handler` \u2014 queues a job |\n| `GET`\/`DELETE \/jobs\/{job_id}` | `job_status_handler` \/ `cancel_job_handler` |\n| `POST \/detect` | `detect_handler` |\n| `GET \/formats` | `formats_handler` |\n| `GET \/health` | `health_handler` |\n| `GET \/info`, `GET \/version` | `info_handler`, `version_handler` |\n| `GET \/cache\/stats` | `cache_stats_handler` |\n| `DELETE \/cache\/clear` | `cache_clear_handler` |\n| `GET \/cache\/manifest`, `POST \/cache\/warm` | `cache_manifest_handler`, `cache_warm_handler` |\n| `PUT \/process`, `POST \/v1\/convert\/file` | `openweb_external_handler`, `openweb_docling_handler` (`api\/openweb.rs`) |\n| `GET \/openapi.json` | `openapi_schema_handler` (feature `api`) |\n| `GET \/metrics` | `metrics_handler` (feature `prometheus`) |\n\nThere is **no** `POST \/extract-url` (URL ingestion is a field on `ExtractInput` passed to\n`\/extract`) and **no** `POST \/batch` (batch is `\/extract-async` + `\/jobs\/{job_id}`).\n\nMiddleware, in order: `DefaultBodyLimit::max(limits.max_request_body_bytes)` +\n`RequestBodyLimitLayer` (default 100 MB), CORS, request-id, compression, catch-panic,\nsensitive-header stripping, tracing. CORS is built explicitly as\n`CorsLayer::new().allow_origin(Any).allow_methods(Any).allow_headers(Any)` and warns loudly;\nrestrict it with `XBERG_CORS_ORIGINS`.\n\n## Caching\n\n`crates\/xberg\/src\/cache\/` \u2014 `GenericCache` is a **filesystem-backed** store with LRU-style\neviction, not an in-memory map. Keys are BLAKE3 content hashes (`blake3_hash_bytes` \/\n`blake3_hash_file`, `cache\/utilities.rs`). Eviction is bounded by `max_age_days`,\n`max_cache_size_mb` and `min_free_space_mb` \u2014 there is no entry-count limit.\n\n## Error handling\n\n`ApiError` is a **struct**, not an enum: `{ status: StatusCode, body: ErrorResponse }`\n(`api\/error.rs`). Status comes from the constructor, not a variant:\n\n- `validation()` \u2192 400, `unprocessable()` \u2192 422, `internal()` \u2192 500, `bad_gateway()` \u2192 502\n- `From<XbergError>` picks one via `error.api_status_category()`\n  (`Validation` \/ `Unprocessable` \/ `Internal`)\n\nThere is no 404, 413 or 503 path with a named variant. Do not `match` on `ApiError`.\n\n## MCP\n\n`crates\/xberg\/src\/mcp\/`. Transport is a single nested rmcp streamable-HTTP service \u2014\n`Router::new().nest_service(\"\/mcp\", http_service)` \u2014 **not** a set of `\/mcp\/*` REST paths.\nTools, resources and prompts are JSON-RPC methods on it. Stdio transport serves the same\nrouter over stdin\/stdout.\n\n### Tools (9)\n\n`extract`, `extract_batch`, `detect_mime_type`, `list_formats`, `cache_stats`, `cache_clear`,\n`get_version`, `cache_manifest`, `cache_warm`. The set is pinned by\n`test_all_tools_are_registered` in `mcp\/server.rs`. There is no `get_capabilities`.\n\n`extract`, `extract_batch` and `cache_warm` are task-eligible (`TASK_ELIGIBLE_TOOLS`,\n`mcp\/server.rs`).\n\n### Resources\n\n`mcp\/resources.rs`: `xberg:\/\/formats`, `xberg:\/\/models`, `xberg:\/\/languages\/ocr`, plus\n`xberg:\/\/presets\/embeddings` behind `#[cfg(feature = \"embeddings\")]`.\n\n### Prompts (3)\n\n`mcp\/prompts.rs`: `extract_document`, `extract_with_ocr`, `semantic_search`.\n\n## Environment variables\n\nThere is no `.env.example`. Server-side vars are read in `core\/server_config\/env.rs`:\n\n- `XBERG_HOST`, `XBERG_PORT` (defaults `127.0.0.1:8000`)\n- `XBERG_MAX_REQUEST_BODY_BYTES`, `XBERG_MAX_MULTIPART_FIELD_BYTES` (both default 100 MB)\n- `XBERG_CORS_ORIGINS` (comma-separated)\n\nExtraction-side vars are documented on `ExtractionConfig::apply_env_overrides`\n(`core\/config\/extraction\/env.rs`) \u2014 `XBERG_OCR_BACKEND`, `XBERG_OCR_LANGUAGE`,\n`XBERG_CHUNKING_MAX_CHARS`, `XBERG_CACHE_ENABLED`, `XBERG_LLM_*`, and others. Read that doc\ncomment rather than guessing a name.\n\n## Critical Rules\n\n### REST\n\n1. **Validate uploads** \u2014 MIME type, size, magic bytes; never trust the filename.\n2. **Size limits are configurable** \u2014 always read `limits.max_request_body_bytes`, never hardcode.\n3. **Errors must be actionable** \u2014 include the operation and a remediation hint in `ErrorResponse`.\n4. **CORS is permissive by default** \u2014 production deployments must set `XBERG_CORS_ORIGINS`.\n5. **Long work goes to `\/extract-async`** \u2014 do not block a request thread on a multi-minute extraction.\n\n### MCP\n\n1. **Register a new tool in `mcp\/server.rs` and extend `test_all_tools_are_registered`** \u2014 the test is the contract.\n2. **Feature-gate resources the same way `xberg:\/\/presets\/embeddings` is** \u2014 a missing feature must not break\n   `resources\/list`.\n3. **Resources are static** \u2014 no network or filesystem scans in a resource handler.\n4. **Tools need timeouts** \u2014 a hung tool blocks the agent.\n\n## Related Skills\n\n- **extraction-pipeline-patterns** \u2014 the core extraction the handlers and tools call\n- **chunking-embeddings** \u2014 optional chunking\/embedding parameters\n- **config-loading-precedence** \u2014 server-mode precedence and env overrides\n","use_cases":["Set up the API server for document extraction and model context processing.","Register and manage MCP tools for remote model interactions.","Configure caching and error handling for the API server."],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/xberg-io\/xberg\/blob\/ffbf7971796a126fc52f6bb8fb40418772071f61\/.ai-rulez\/skills\/api-server-mcp\/SKILL.md","source_commit":"ffbf7971796a126fc52f6bb8fb40418772071f61","license":null,"version":null,"listing_approved_at":"2026-11-16","created_at":"2026-11-15T13:14:23.000000Z","updated_at":"2026-11-16T05:16:54.000000Z","seo_title":"API Server & MCP Protocol Setup","seo_description":"Configure the API server and MCP protocol for document extraction and model context processing with Axum and rmcp.","seo_keywords":["API server setup","MCP protocol integration","document extraction API","model context processing","Axum server configuration","rmcp tool registration"]}}