-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add MCP context local tool adapter #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,141 @@ | ||||||||||||||||||||||||||||||||||||||||||
| """One-shot JSON tool adapter for the deterministic MCP context layer.""" | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| import argparse | ||||||||||||||||||||||||||||||||||||||||||
| import json | ||||||||||||||||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||||||||||||||||||||||||
| from typing import Any | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| REPO_ROOT = Path(__file__).resolve().parents[1] | ||||||||||||||||||||||||||||||||||||||||||
| if str(REPO_ROOT) not in sys.path: | ||||||||||||||||||||||||||||||||||||||||||
| sys.path.insert(0, str(REPO_ROOT)) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| from scripts.mcp_context_cli import load_fixture_context | ||||||||||||||||||||||||||||||||||||||||||
| from src.comptext_v7.mcp import build_replay_payload, render_prompt_context, validate_replay_payload | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def _json_response(payload: dict[str, Any]) -> str: | ||||||||||||||||||||||||||||||||||||||||||
| return json.dumps(payload, indent=2, sort_keys=True) + "\n" | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def _load_request(path: Path | None) -> dict[str, Any]: | ||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||
| raw = path.read_text(encoding="utf-8") if path is not None else sys.stdin.read() | ||||||||||||||||||||||||||||||||||||||||||
| except FileNotFoundError as exc: | ||||||||||||||||||||||||||||||||||||||||||
| raise RuntimeError(f"missing request file: {path.as_posix() if path is not None else '<stdin>'}") from exc | ||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||
| request = json.loads(raw) | ||||||||||||||||||||||||||||||||||||||||||
| except json.JSONDecodeError as exc: | ||||||||||||||||||||||||||||||||||||||||||
| raise RuntimeError(f"invalid JSON request: {exc}") from exc | ||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(request, dict): | ||||||||||||||||||||||||||||||||||||||||||
| raise RuntimeError("request must be a JSON object") | ||||||||||||||||||||||||||||||||||||||||||
| return request | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def _params(request: dict[str, Any]) -> dict[str, Any]: | ||||||||||||||||||||||||||||||||||||||||||
| params = request.get("params", {}) | ||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(params, dict): | ||||||||||||||||||||||||||||||||||||||||||
| raise RuntimeError("request.params must be a JSON object") | ||||||||||||||||||||||||||||||||||||||||||
| return params | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def _tool_name(request: dict[str, Any]) -> str: | ||||||||||||||||||||||||||||||||||||||||||
| tool = request.get("tool") | ||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(tool, str) or not tool: | ||||||||||||||||||||||||||||||||||||||||||
| raise RuntimeError("request.tool must be a non-empty string") | ||||||||||||||||||||||||||||||||||||||||||
| return tool | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def _payload_from_params(params: dict[str, Any]) -> dict[str, Any]: | ||||||||||||||||||||||||||||||||||||||||||
| payload = params.get("payload") | ||||||||||||||||||||||||||||||||||||||||||
| if isinstance(payload, dict): | ||||||||||||||||||||||||||||||||||||||||||
| return payload | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| fixture = params.get("fixture") | ||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(fixture, str) or not fixture: | ||||||||||||||||||||||||||||||||||||||||||
| raise RuntimeError("request.params requires payload object or fixture path") | ||||||||||||||||||||||||||||||||||||||||||
| return build_replay_payload(load_fixture_context(Path(fixture))) | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+51
to
+59
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To maintain strictness as per the general rules, we should explicitly validate that
Suggested change
References
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def _handle_build_replay_payload(params: dict[str, Any]) -> dict[str, Any]: | ||||||||||||||||||||||||||||||||||||||||||
| fixture = params.get("fixture") | ||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(fixture, str) or not fixture: | ||||||||||||||||||||||||||||||||||||||||||
| raise RuntimeError("build_replay_payload requires params.fixture") | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| payload = build_replay_payload(load_fixture_context(Path(fixture))) | ||||||||||||||||||||||||||||||||||||||||||
| result: dict[str, Any] = {"payload": payload} | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| validation = validate_replay_payload(payload) if params.get("validate") is True else None | ||||||||||||||||||||||||||||||||||||||||||
| if validation is not None: | ||||||||||||||||||||||||||||||||||||||||||
| result["validation"] = validation | ||||||||||||||||||||||||||||||||||||||||||
| if params.get("render_prompt") is True: | ||||||||||||||||||||||||||||||||||||||||||
| prompt_payload = {**payload} | ||||||||||||||||||||||||||||||||||||||||||
| if validation is not None: | ||||||||||||||||||||||||||||||||||||||||||
| prompt_payload["validation"] = validation | ||||||||||||||||||||||||||||||||||||||||||
| result["prompt_context"] = render_prompt_context(prompt_payload) | ||||||||||||||||||||||||||||||||||||||||||
| return result | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def _handle_render_prompt_context(params: dict[str, Any]) -> dict[str, Any]: | ||||||||||||||||||||||||||||||||||||||||||
| payload = _payload_from_params(params) | ||||||||||||||||||||||||||||||||||||||||||
| if params.get("validate") is True: | ||||||||||||||||||||||||||||||||||||||||||
| payload = {**payload, "validation": validate_replay_payload(payload)} | ||||||||||||||||||||||||||||||||||||||||||
| return {"prompt_context": render_prompt_context(payload)} | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def _handle_validate_replay_payload(params: dict[str, Any]) -> dict[str, Any]: | ||||||||||||||||||||||||||||||||||||||||||
| return {"validation": validate_replay_payload(_payload_from_params(params))} | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def handle_request(request: dict[str, Any]) -> dict[str, Any]: | ||||||||||||||||||||||||||||||||||||||||||
| tool = _tool_name(request) | ||||||||||||||||||||||||||||||||||||||||||
| params = _params(request) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if tool == "build_replay_payload": | ||||||||||||||||||||||||||||||||||||||||||
| result = _handle_build_replay_payload(params) | ||||||||||||||||||||||||||||||||||||||||||
| elif tool == "render_prompt_context": | ||||||||||||||||||||||||||||||||||||||||||
| result = _handle_render_prompt_context(params) | ||||||||||||||||||||||||||||||||||||||||||
| elif tool == "validate_replay_payload": | ||||||||||||||||||||||||||||||||||||||||||
| result = _handle_validate_replay_payload(params) | ||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||
| raise RuntimeError(f"unsupported tool: {tool}") | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return {"ok": True, "result": result, "tool": tool} | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def _parse_args(argv: list[str]) -> argparse.Namespace: | ||||||||||||||||||||||||||||||||||||||||||
| parser = argparse.ArgumentParser(description="Run one deterministic MCP context-layer tool request.") | ||||||||||||||||||||||||||||||||||||||||||
| parser.add_argument("--request-file", type=Path, help="JSON request file. Reads stdin when omitted.") | ||||||||||||||||||||||||||||||||||||||||||
| return parser.parse_args(argv) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def main(argv: list[str] | None = None) -> int: | ||||||||||||||||||||||||||||||||||||||||||
| args = _parse_args(sys.argv[1:] if argv is None else argv) | ||||||||||||||||||||||||||||||||||||||||||
| tool: str | None = None | ||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||
| request = _load_request(args.request_file) | ||||||||||||||||||||||||||||||||||||||||||
| request_tool = request.get("tool") | ||||||||||||||||||||||||||||||||||||||||||
| tool = request_tool if isinstance(request_tool, str) else None | ||||||||||||||||||||||||||||||||||||||||||
| response = handle_request(request) | ||||||||||||||||||||||||||||||||||||||||||
| sys.stdout.write(_json_response(response)) | ||||||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||||||
| except Exception as exc: | ||||||||||||||||||||||||||||||||||||||||||
| sys.stdout.write( | ||||||||||||||||||||||||||||||||||||||||||
| _json_response( | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| "error": { | ||||||||||||||||||||||||||||||||||||||||||
| "message": str(exc), | ||||||||||||||||||||||||||||||||||||||||||
| "type": exc.__class__.__name__, | ||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||
| "ok": False, | ||||||||||||||||||||||||||||||||||||||||||
| "tool": tool, | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||||||||||||||||||
| raise SystemExit(main()) | ||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error handling for loading the request JSON should include the repo-relative path in the
RuntimeErrormessage for bothFileNotFoundErrorandJSONDecodeError, as per the general rules. Additionally, the type check for the request object should also include the path for better diagnostics.References