Skip to content

feat(mcp): add the MCP server as a standalone Python service - #165

Closed
oto-macenauer-absa wants to merge 1 commit into
masterfrom
feature/117-mcp-python-server
Closed

oto-macenauer-absa wants to merge 1 commit into
masterfrom
feature/117-mcp-python-server

Conversation

@oto-macenauer-absa

@oto-macenauer-absa oto-macenauer-absa commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

What

Second of three PRs for #117. This one adds the MCP server as a self-contained Python project in mcp-server/, built on the official MCP Python SDK (v2, pinned exactly). It shares no code with the site. It reads dist/_kb/pages.json (from the previous PR) and the HTML under dist/. It is not deployed yet; the next PR does that.

Please review the ADR first: docs/adr/0001-mcp-server.md. It covers:

  • the decision, with the considered options and their pros and cons;
  • why it is a separate Python service and not a hybrid;
  • the layering;
  • security ownership;
  • the image and deployment shape.

Stack: #164 (page manifest) ← #165 (this PR, Python MCP server) ← #166 (deployment).

Changes

  • kb-mcp index (build time only, indexer extra):
    • Extracts every page with selectolax, an HTML5 parser, into a SQLite FTS5 index.
    • Ranking: Porter stemming, and bm25 with the title weighted highest.
    • Query expansion uses the vocabulary of an unstemmed table, covering compound splitting and misspellings (difflib).
    • The indexer writes to a temporary file, then atomically replaces the index.
  • kb-mcp serve: stateless Streamable HTTP with JSON responses.
    • Tools: search_documents, list_documents and read_document, all read-only, with structured output and cursor pagination.
    • Every page is also a kb:// resource.
    • The server's instructions list the apps.
  • Layout (ports and adapters):
    • domain, ports, a service facade, adapters (SQLite, query, cursor), protocol and http.
    • Layering is enforced by import-linter: the runtime never imports the indexer or its HTML parser.
  • HTTP:
    • The SDK checks Host and Origin (TransportSecuritySettings) and caps the body at 64 KiB.
    • Starlette CORS for configured origins only.
    • /healthz reports the index's content hash.
    • A browser opening the endpoint gets a 405 page explaining it.
  • Settings: pydantic-settings reads KB_MCP_*; it is the only code that reads the environment.
  • Quality gates:
    • ruff (lint and format), mypy --strict, import-linter;
    • pytest with unit, backend contract, in-process protocol (the SDK's own client) and HTTP tests, and a 90% branch coverage gate.
  • CI: a new mcp job runs all of the above, then indexes the real fixture build.
  • Dependabot: uv ecosystem for /mcp-server.
  • Docs: docs/mcp-development.md, plus AGENTS.md and CLAUDE.md.

Release Notes

  • Added an MCP server (Python, mcp-server/) that lets AI agents search and read the knowledge base: search_documents, list_documents, read_document, and every page as a kb:// resource.
  • Added ADR 0001, which records why the server is a standalone Python service and how it is built, secured and deployed.

Verification

  • uv run ruff format --check . && uv run ruff check . && uv run mypy && uv run lint-imports: clean
  • uv run pytest: 78 passed, 95% branch coverage
  • uv run kb-mcp index --dist ../dist: indexes the real fixture build

Part of #117

🤖 Generated with Claude Code

mcp-server/ is a self-contained uv project on the official MCP Python SDK
(v2, pinned). It shares no code with the site: it reads the page manifest
and the HTML under dist/ (docs/adr/0001-mcp-server.md).

- `kb-mcp index` extracts every page (selectolax, an HTML5 parser) into a
  SQLite FTS5 index: Porter stemming, bm25 with the title weighted highest,
  compound-word and misspelling expansion over an unstemmed vocabulary;
- `kb-mcp serve` serves it over stateless Streamable HTTP with JSON
  responses: search_documents, list_documents, read_document and every page
  as a kb:// resource, with structured output and read-only annotations;
- ports and adapters as before (DocumentRepository, SearchBackend, the
  KnowledgeBase facade), with the layering enforced by import-linter;
- quality gates: ruff, mypy --strict, pytest (unit, backend contract,
  in-process protocol and HTTP tests, 90% branch coverage), a CI job that
  also indexes the real fixture build, and Dependabot for uv.

Part of #117

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
oto-macenauer-absa added a commit that referenced this pull request Nov 17, 2026
…fest (#164)

## What

First of three PRs that rebuild the MCP server (#117) as a standalone
Python service. The ADR comes in the next PR
(`docs/adr/0001-mcp-server.md`). The TypeScript implementation (#119,
#129) is closed as superseded.

The Python indexer must not import the build's JavaScript. So the build
no longer indexes pages; it writes a manifest of what it published. That
manifest, plus the HTML under `dist/`, is the only interface between the
Node build and the Python server.

Stack: **#164 (this PR, page manifest)** ← #165 (Python MCP server) ←
#166 (deployment).

## Changes

- **New build step 3b** (`scripts/page-manifest.js`): writes
`dist/_kb/pages.json`.
- Its shape is `scripts/page-manifest.schema.json` (draft-07, no extra
properties).
- It has one entry per published route: route, dist file, manifest
title, section, and the page's app.
- Each app carries its name, description, tags, kind (artifact or
iframe), url and source version.
- `404.html` is excluded, and routes are deduplicated (`index.html`
wins).
- **Removed**, from the merged #128:
  - the TypeScript index builder (`mcp/`, `scripts/build-mcp.js`);
  - its in-memory adapters, tests and fixtures;
- the ESLint/Prettier/tsc lint tooling that only covered that code,
including the CI `lint` job and those devDependencies.
- **`.dockerignore`** keeps `dist/_kb` out of the nginx image.
- **Tests:** new cases in `tests/build-integrity.spec.js` check that:
  - the manifest validates against its schema;
  - it lists the expected apps, with iframe kind and url;
  - routes are unique and sorted, and every listed file exists;
  - sectioned pages carry their titles.
- **CLAUDE.md:** documents the new pipeline step and file.

## Release Notes

- The site build writes `dist/_kb/pages.json`, a manifest of every
published page and its app. The Python MCP server's indexer reads it.
- Removed the TypeScript MCP search index and the lint tooling that only
covered it.

## Verification

- `npm test`: all passed
- `npm audit --omit=dev --audit-level=high`: clean

Part of #117

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@oto-macenauer-absa
oto-macenauer-absa force-pushed the feature/117-mcp-python-server branch from 54ad859 to d4a18a5 Compare November 17, 2026 14:19
@oto-macenauer-absa
oto-macenauer-absa removed this pull request from stack #167 November 17, 2026 14:21
@oto-macenauer-absa

Copy link
Copy Markdown
Collaborator Author

Joined into #166, which now targets master and carries both commit sets for a single review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant