mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-03 17:11:24 +00:00
The backend import package is now docsgpt, the name it will carry on PyPI; application was far too generic to install into anyone's site-packages. git mv plus a mechanical rewrite of every import, dotted string and path reference: 734 Python files, the compose files, Dockerfile, workflows, docs, setup scripts, devcontainer, k8s manifests, vscode config, pytest and coverage config, .gitignore. Behaviour is unchanged. Kept for one release: - A top-level application package whose meta-path finder resolves application.x.y to the already-imported docsgpt.x.y object, so old imports and entry points (celery -A application.app.celery, uvicorn application.asgi:asgi_app) keep working with a FutureWarning. - Celery registers every application.* task name as an alias of its docsgpt.* task on start-up, so messages queued by the previous release still run. The redbeat key prefix moves to redbeat:docsgpt:v2: so schedule entries the previous release wrote are left unread instead of firing twice. The backend image builds from the repository root (docker build -f docsgpt/Dockerfile .) so it can ship the alias package; a root .dockerignore allow-lists docsgpt/ and application/ and keeps caches, local data, .env files, the sample index files and the Dockerfile out. Compose and the image workflows point at the new context.
62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
"""FastMCP server exposing DocsGPT retrieval over streamable HTTP.
|
|
|
|
Mounted at ``/mcp`` by ``docsgpt/asgi.py``. Bearer tokens are the
|
|
existing DocsGPT agent API keys — no new credential surface. The
|
|
``search_docs`` tool searches the caller's knowledge base.
|
|
|
|
The tool reads the ``Authorization`` header directly via
|
|
``get_http_headers(include={"authorization"})``. The ``include`` kwarg
|
|
is required: by default ``get_http_headers`` strips ``authorization``
|
|
(and a handful of other hop-by-hop headers) so they aren't forwarded
|
|
to downstream services — since we deliberately want the caller's
|
|
token, we opt it back in.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
|
|
from fastmcp import FastMCP
|
|
from fastmcp.server.dependencies import get_http_headers
|
|
|
|
from docsgpt.services.search_service import (
|
|
InvalidAPIKey,
|
|
SearchFailed,
|
|
search,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _extract_bearer_token() -> str | None:
|
|
auth = get_http_headers(include={"authorization"}).get("authorization", "")
|
|
parts = auth.split(None, 1)
|
|
if len(parts) != 2 or parts[0].lower() != "bearer" or not parts[1]:
|
|
return None
|
|
return parts[1]
|
|
|
|
|
|
mcp = FastMCP("docsgpt")
|
|
|
|
|
|
@mcp.tool
|
|
async def search_docs(query: str, chunks: int = 5) -> list[dict]:
|
|
"""Search the caller's DocsGPT knowledge base.
|
|
|
|
Authentication is via ``Authorization: Bearer <agent-api-key>`` on
|
|
the MCP request — the same opaque key that ``/api/search`` accepts
|
|
in its JSON body. Returns at most ``chunks`` hits, each a dict with
|
|
``text``, ``title``, ``source`` keys.
|
|
"""
|
|
api_key = _extract_bearer_token()
|
|
if not api_key:
|
|
raise PermissionError("Missing Bearer token")
|
|
try:
|
|
return await asyncio.to_thread(search, api_key, query, chunks)
|
|
except InvalidAPIKey as exc:
|
|
raise PermissionError("Invalid API key") from exc
|
|
except SearchFailed:
|
|
logger.exception("search_docs failed")
|
|
raise
|