A tiny, local-first CLI that finds the files your AI coding agent actually needs — and packs them into a focused context.
| 1 | """MCP server tests.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import io |
| 6 | import json |
| 7 | from pathlib import Path |
| 8 | |
| 9 | from agent_context.mcp import McpServer, TOOLS |
| 10 | |
| 11 | |
| 12 | def _exchange(server: McpServer, messages: list[dict]) -> list[dict]: |
| 13 | stdin = io.StringIO("\n".join(json.dumps(m) for m in messages) + "\n") |
| 14 | stdout = io.StringIO() |
| 15 | server.stdin = stdin |
| 16 | server.stdout = stdout |
| 17 | server.run() |
| 18 | lines = [ln for ln in stdout.getvalue().splitlines() if ln.strip()] |
| 19 | return [json.loads(ln) for ln in lines] |
| 20 | |
| 21 | |
| 22 | def test_tool_registration(): |
| 23 | names = {t["name"] for t in TOOLS} |
| 24 | assert names == { |
| 25 | "analyze_repository", |
| 26 | "find_relevant_files", |
| 27 | "build_context", |
| 28 | "repository_map", |
| 29 | } |
| 30 | |
| 31 | |
| 32 | def test_initialize_and_list_tools(sample_repo: Path): |
| 33 | server = McpServer(cwd=sample_repo) |
| 34 | responses = _exchange( |
| 35 | server, |
| 36 | [ |
| 37 | { |
| 38 | "jsonrpc": "2.0", |
| 39 | "id": 1, |
| 40 | "method": "initialize", |
| 41 | "params": { |
| 42 | "protocolVersion": "2024-11-05", |
| 43 | "capabilities": {}, |
| 44 | "clientInfo": {"name": "test", "version": "0"}, |
| 45 | }, |
| 46 | }, |
| 47 | {"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}, |
| 48 | ], |
| 49 | ) |
| 50 | assert responses[0]["result"]["serverInfo"]["name"] == "agent-context" |
| 51 | tool_names = {t["name"] for t in responses[1]["result"]["tools"]} |
| 52 | assert "analyze_repository" in tool_names |
| 53 | |
| 54 | |
| 55 | def test_analyze_and_find(sample_repo: Path): |
| 56 | server = McpServer(cwd=sample_repo) |
| 57 | responses = _exchange( |
| 58 | server, |
| 59 | [ |
| 60 | { |
| 61 | "jsonrpc": "2.0", |
| 62 | "id": 1, |
| 63 | "method": "tools/call", |
| 64 | "params": { |
| 65 | "name": "analyze_repository", |
| 66 | "arguments": {"path": str(sample_repo)}, |
| 67 | }, |
| 68 | }, |
| 69 | { |
| 70 | "jsonrpc": "2.0", |
| 71 | "id": 2, |
| 72 | "method": "tools/call", |
| 73 | "params": { |
| 74 | "name": "find_relevant_files", |
| 75 | "arguments": { |
| 76 | "task": "authentication middleware", |
| 77 | "path": str(sample_repo), |
| 78 | "limit": 5, |
| 79 | }, |
| 80 | }, |
| 81 | }, |
| 82 | ], |
| 83 | ) |
| 84 | analyze = json.loads(responses[0]["result"]["content"][0]["text"]) |
| 85 | assert analyze["files"]["total"] >= 1 |
| 86 | # Secret names may appear as paths, but never values |
| 87 | blob = json.dumps(analyze) |
| 88 | assert "super-secret-key-12345" not in blob |
| 89 | |
| 90 | found = json.loads(responses[1]["result"]["content"][0]["text"]) |
| 91 | assert found["files"] |
| 92 | assert "auth" in found["files"][0]["path"] |
| 93 | |
| 94 | |
| 95 | def test_build_context_and_map(sample_repo: Path): |
| 96 | server = McpServer(cwd=sample_repo) |
| 97 | responses = _exchange( |
| 98 | server, |
| 99 | [ |
| 100 | { |
| 101 | "jsonrpc": "2.0", |
| 102 | "id": 1, |
| 103 | "method": "tools/call", |
| 104 | "params": { |
| 105 | "name": "build_context", |
| 106 | "arguments": { |
| 107 | "task": "fix auth", |
| 108 | "path": str(sample_repo), |
| 109 | "tokens": 4000, |
| 110 | "write": False, |
| 111 | }, |
| 112 | }, |
| 113 | }, |
| 114 | { |
| 115 | "jsonrpc": "2.0", |
| 116 | "id": 2, |
| 117 | "method": "tools/call", |
| 118 | "params": { |
| 119 | "name": "repository_map", |
| 120 | "arguments": {"path": str(sample_repo)}, |
| 121 | }, |
| 122 | }, |
| 123 | ], |
| 124 | ) |
| 125 | built = json.loads(responses[0]["result"]["content"][0]["text"]) |
| 126 | assert "markdown" in built |
| 127 | assert "super-secret-key-12345" not in built["markdown"] |
| 128 | assert "hunter2" not in built["markdown"] |
| 129 | |
| 130 | repo_map = json.loads(responses[1]["result"]["content"][0]["text"]) |
| 131 | assert "detected_technology" in repo_map |
| 132 | |
| 133 | |
| 134 | def test_unknown_tool_error(sample_repo: Path): |
| 135 | server = McpServer(cwd=sample_repo) |
| 136 | responses = _exchange( |
| 137 | server, |
| 138 | [ |
| 139 | { |
| 140 | "jsonrpc": "2.0", |
| 141 | "id": 1, |
| 142 | "method": "tools/call", |
| 143 | "params": {"name": "nope", "arguments": {}}, |
| 144 | } |
| 145 | ], |
| 146 | ) |
| 147 | assert "error" in responses[0] |