feat(mcp): add OpenCode-backed wiki query

Add a query tool group for the wiki-file-assets MCP server that queues fixed /wiki-query OpenCode jobs, exposes status/result/cancel/readiness tools, and keeps query disabled until explicitly enabled.

Document the new service switches and config, keep remote query gated in AGENTS.md, preserve token behavior, and pass proxy environment through the managed service so OpenCode smoke/query jobs can reach the model provider.

Validation: python3 -m unittest discover -v; python3 -m py_compile obsidian_wiki/mcp_query.py obsidian_wiki/mcp_server.py scripts/manage_mcp_service.py tests/test_mcp_query.py tests/test_mcp_server_remote.py tests/test_mcp_service.py; git diff --check.
This commit is contained in:
Codex
2026-07-05 18:00:53 +00:00
parent c8433a2e2e
commit e7913d4c6b
9 changed files with 1085 additions and 12 deletions
+103 -3
View File
@@ -25,6 +25,7 @@ from obsidian_wiki.mcp_ingest import (
IngestManager,
parse_bool,
)
from obsidian_wiki.mcp_query import DEFAULT_QUERY_MAX_CHARS, QueryError, QueryManager
from obsidian_wiki.mcp_assets import (
DEFAULT_MAX_FILE_BYTES,
DEFAULT_PROFILE_NAME,
@@ -46,8 +47,9 @@ DEFAULT_TOKEN_ENV = "OBSIDIAN_MCP_TOKEN"
MCP_SERVER_NAME = "wiki-file-assets"
TOOL_FILE_ASSETS = "file-assets"
TOOL_INGEST = "ingest"
TOOL_QUERY = "query"
DEFAULT_ENABLED_TOOLS = {TOOL_FILE_ASSETS}
AVAILABLE_TOOLS = {TOOL_FILE_ASSETS, TOOL_INGEST}
AVAILABLE_TOOLS = {TOOL_FILE_ASSETS, TOOL_INGEST, TOOL_QUERY}
def normalize_tool_name(name: str) -> str:
@@ -62,6 +64,11 @@ def normalize_tool_name(name: str) -> str:
"upload": TOOL_INGEST,
"uploads": TOOL_INGEST,
"opencode-ingest": TOOL_INGEST,
"query": TOOL_QUERY,
"wiki-query": TOOL_QUERY,
"ask": TOOL_QUERY,
"question": TOOL_QUERY,
"questions": TOOL_QUERY,
}
if normalized not in aliases:
raise ValueError(f"unknown MCP tool group: {name}")
@@ -141,13 +148,17 @@ def create_server(
allowed_origins: list[str] | None = None,
enable_file_assets: bool = True,
enable_ingest: bool = False,
enable_query: bool = False,
project_root: str | Path | None = None,
ingest_runtime_root: str | Path | None = None,
query_runtime_root: str | Path | None = None,
opencode_path: str | Path = DEFAULT_OPENCODE_PATH,
ingest_max_file_bytes: int = DEFAULT_INGEST_MAX_FILE_BYTES,
ingest_max_chunk_bytes: int = DEFAULT_INGEST_MAX_CHUNK_BYTES,
ingest_max_files_per_job: int = DEFAULT_INGEST_MAX_FILES_PER_JOB,
query_max_chars: int = DEFAULT_QUERY_MAX_CHARS,
ingest_manager: IngestManager | None = None,
query_manager: QueryManager | None = None,
):
if sys.version_info < (3, 10):
raise RuntimeError("obsidian-wiki MCP server requires Python 3.10 or newer")
@@ -168,6 +179,11 @@ def create_server(
if ingest_runtime_root is not None
else project / ".runtime" / "mcp-ingest"
)
query_runtime = (
Path(query_runtime_root).expanduser().resolve()
if query_runtime_root is not None
else project / ".runtime" / "mcp-query"
)
transport_security = None
if TransportSecuritySettings is not None and (allowed_hosts is not None or allowed_origins is not None):
transport_security = TransportSecuritySettings(
@@ -199,8 +215,10 @@ def create_server(
"enabled_tools": sorted(
([TOOL_FILE_ASSETS] if enable_file_assets else [])
+ ([TOOL_INGEST] if enable_ingest else [])
+ ([TOOL_QUERY] if enable_query else [])
),
"ingest_enabled": enable_ingest,
"query_enabled": enable_query,
}
if enable_ingest:
@@ -313,6 +331,51 @@ def create_server(
"""Check whether local OpenCode can load the wiki-ingest skill for MCP ingest jobs."""
return ingest_manager.check_opencode_readiness(run_smoke_test=run_smoke_test)
if enable_query:
if query_manager is None:
query_manager = QueryManager(
vault_path=vault,
project_root=project,
runtime_root=query_runtime,
opencode_path=opencode_path,
max_query_chars=query_max_chars,
)
@server.tool()
def start_wiki_query(
query: str,
mode: str = "normal",
public_only: bool = False,
note: str | None = None,
) -> dict[str, Any]:
"""Start or queue a local OpenCode wiki-query job."""
return query_manager.start_query_job(
query,
mode=mode,
public_only=public_only,
note=note,
)
@server.tool()
def get_wiki_query_status(job_id: str) -> dict[str, Any]:
"""Return state, log tail, and exit status for an MCP wiki query job."""
return query_manager.get_query_job_status(job_id)
@server.tool()
def get_wiki_query_result(job_id: str) -> dict[str, Any]:
"""Return the final or current answer for an MCP wiki query job."""
return query_manager.get_query_job_result(job_id)
@server.tool()
def cancel_wiki_query(job_id: str) -> dict[str, Any]:
"""Cancel a queued or running MCP wiki query job."""
return query_manager.cancel_query_job(job_id)
@server.tool()
def check_query_opencode_readiness(run_smoke_test: bool = False) -> dict[str, Any]:
"""Check whether local OpenCode can load the wiki-query skill for MCP query jobs."""
return query_manager.check_opencode_readiness(run_smoke_test=run_smoke_test)
if enable_file_assets:
@server.tool()
@@ -636,12 +699,15 @@ def run_http_server(
allowed_origins: list[str] | None,
enable_file_assets: bool = True,
enable_ingest: bool = False,
enable_query: bool = False,
project_root: str | Path | None = None,
ingest_runtime_root: str | Path | None = None,
query_runtime_root: str | Path | None = None,
opencode_path: str | Path = DEFAULT_OPENCODE_PATH,
ingest_max_file_bytes: int = DEFAULT_INGEST_MAX_FILE_BYTES,
ingest_max_chunk_bytes: int = DEFAULT_INGEST_MAX_CHUNK_BYTES,
ingest_max_files_per_job: int = DEFAULT_INGEST_MAX_FILES_PER_JOB,
query_max_chars: int = DEFAULT_QUERY_MAX_CHARS,
) -> None:
token = read_token_from_env(token_env)
try:
@@ -667,6 +733,22 @@ def run_http_server(
max_files_per_job=ingest_max_files_per_job,
)
query_manager = None
if enable_query:
project = Path(project_root).expanduser().resolve() if project_root is not None else Path.cwd().resolve()
query_runtime = (
Path(query_runtime_root).expanduser().resolve()
if query_runtime_root is not None
else project / ".runtime" / "mcp-query"
)
query_manager = QueryManager(
vault_path=vault_path,
project_root=project,
runtime_root=query_runtime,
opencode_path=opencode_path,
max_query_chars=query_max_chars,
)
server = create_server(
vault_path=vault_path,
db_path=db_path,
@@ -680,13 +762,17 @@ def run_http_server(
allowed_origins=allowed_origins,
enable_file_assets=enable_file_assets,
enable_ingest=enable_ingest,
enable_query=enable_query,
project_root=project_root,
ingest_runtime_root=ingest_runtime_root,
query_runtime_root=query_runtime_root,
opencode_path=opencode_path,
ingest_max_file_bytes=ingest_max_file_bytes,
ingest_max_chunk_bytes=ingest_max_chunk_bytes,
ingest_max_files_per_job=ingest_max_files_per_job,
query_max_chars=query_max_chars,
ingest_manager=ingest_manager,
query_manager=query_manager,
)
app = create_http_app(server, token, ingest_manager=ingest_manager)
uvicorn.run(app, host=host, port=port, log_level="info")
@@ -740,17 +826,25 @@ def main(argv: list[str] | None = None) -> int:
enabled_tools = enabled_tool_names()
enable_file_assets = TOOL_FILE_ASSETS in enabled_tools
enable_ingest = TOOL_INGEST in enabled_tools
enable_query = TOOL_QUERY in enabled_tools
ingest_max_file_bytes = runtime_config_int("OBSIDIAN_MCP_INGEST_MAX_FILE_BYTES", DEFAULT_INGEST_MAX_FILE_BYTES)
ingest_max_chunk_bytes = runtime_config_int("OBSIDIAN_MCP_INGEST_MAX_CHUNK_BYTES", DEFAULT_INGEST_MAX_CHUNK_BYTES)
ingest_max_files_per_job = runtime_config_int(
"OBSIDIAN_MCP_INGEST_MAX_FILES_PER_JOB",
DEFAULT_INGEST_MAX_FILES_PER_JOB,
)
opencode_path = runtime_config_value("OBSIDIAN_MCP_INGEST_OPENCODE_PATH", DEFAULT_OPENCODE_PATH)
opencode_path = runtime_config_value("OBSIDIAN_MCP_OPENCODE_PATH", "")
if not opencode_path:
opencode_path = runtime_config_value("OBSIDIAN_MCP_INGEST_OPENCODE_PATH", DEFAULT_OPENCODE_PATH)
ingest_runtime_root = runtime_config_value(
"OBSIDIAN_MCP_INGEST_RUNTIME_ROOT",
str(Path.cwd() / ".runtime" / "mcp-ingest"),
)
query_runtime_root = runtime_config_value(
"OBSIDIAN_MCP_QUERY_RUNTIME_ROOT",
str(Path.cwd() / ".runtime" / "mcp-query"),
)
query_max_chars = runtime_config_int("OBSIDIAN_MCP_QUERY_MAX_CHARS", DEFAULT_QUERY_MAX_CHARS)
try:
if transport == "stdio":
server = create_server(
@@ -761,12 +855,15 @@ def main(argv: list[str] | None = None) -> int:
expose_paths=options["expose_paths"],
enable_file_assets=enable_file_assets,
enable_ingest=enable_ingest,
enable_query=enable_query,
project_root=Path.cwd(),
ingest_runtime_root=ingest_runtime_root,
query_runtime_root=query_runtime_root,
opencode_path=opencode_path,
ingest_max_file_bytes=ingest_max_file_bytes,
ingest_max_chunk_bytes=ingest_max_chunk_bytes,
ingest_max_files_per_job=ingest_max_files_per_job,
query_max_chars=query_max_chars,
)
try:
server.run(transport="stdio")
@@ -789,14 +886,17 @@ def main(argv: list[str] | None = None) -> int:
allowed_origins=args.allowed_origins,
enable_file_assets=enable_file_assets,
enable_ingest=enable_ingest,
enable_query=enable_query,
project_root=Path.cwd(),
ingest_runtime_root=ingest_runtime_root,
query_runtime_root=query_runtime_root,
opencode_path=opencode_path,
ingest_max_file_bytes=ingest_max_file_bytes,
ingest_max_chunk_bytes=ingest_max_chunk_bytes,
ingest_max_files_per_job=ingest_max_files_per_job,
query_max_chars=query_max_chars,
)
except (RuntimeError, FileNotFoundError, AssetAccessError, AssetNotFoundError, AssetTooLargeError, IngestError) as exc:
except (RuntimeError, FileNotFoundError, AssetAccessError, AssetNotFoundError, AssetTooLargeError, IngestError, QueryError) as exc:
print(f"error: {exc}", file=sys.stderr)
return 1
return 0