Navegador

navegador / navegador / mcp / server.py
Source Blame History 318 lines
5e4b8e4… anonymous 1 """
5e4b8e4… anonymous 2 Navegador MCP server — exposes graph context tools to AI coding agents.
5e4b8e4… anonymous 3
5e4b8e4… anonymous 4 Run:
5e4b8e4… anonymous 5 navegador mcp --db .navegador/graph.db
5e4b8e4… anonymous 6 """
5e4b8e4… anonymous 7
5e4b8e4… anonymous 8 import json
5e4b8e4… anonymous 9 import logging
758c6b7… lmata 10 from typing import Any
5e4b8e4… anonymous 11
5e4b8e4… anonymous 12 logger = logging.getLogger(__name__)
5e4b8e4… anonymous 13
5e4b8e4… anonymous 14
b3b8664… lmata 15 def create_mcp_server(store_factory, read_only: bool = False):
5e4b8e4… anonymous 16 """
5e4b8e4… anonymous 17 Build and return an MCP server instance wired to a GraphStore factory.
5e4b8e4… anonymous 18
5e4b8e4… anonymous 19 Args:
5e4b8e4… anonymous 20 store_factory: Callable[[], GraphStore] — called lazily on first request.
b3b8664… lmata 21 read_only: When True, the ingest_repo tool is disabled and all
b3b8664… lmata 22 query_graph queries are validated for write operations and
b3b8664… lmata 23 injection patterns. Complexity checks apply to all modes.
5e4b8e4… anonymous 24 """
5e4b8e4… anonymous 25 try:
5e4b8e4… anonymous 26 from mcp.server import Server # type: ignore[import]
5e4b8e4… anonymous 27 from mcp.types import TextContent, Tool # type: ignore[import]
5e4b8e4… anonymous 28 except ImportError as e:
5e4b8e4… anonymous 29 raise ImportError("Install mcp: pip install mcp") from e
5e4b8e4… anonymous 30
5e4b8e4… anonymous 31 from navegador.context import ContextLoader
b3b8664… lmata 32 from navegador.mcp.security import check_complexity, validate_cypher
5e4b8e4… anonymous 33
5e4b8e4… anonymous 34 server = Server("navegador")
758c6b7… lmata 35 _store: Any = None
758c6b7… lmata 36 _loader: ContextLoader | None = None
5e4b8e4… anonymous 37
5e4b8e4… anonymous 38 def _get_loader() -> ContextLoader:
5e4b8e4… anonymous 39 nonlocal _store, _loader
5e4b8e4… anonymous 40 if _loader is None:
5e4b8e4… anonymous 41 _store = store_factory()
5e4b8e4… anonymous 42 _loader = ContextLoader(_store)
5e4b8e4… anonymous 43 return _loader
5e4b8e4… anonymous 44
5e4b8e4… anonymous 45 @server.list_tools()
5e4b8e4… anonymous 46 async def list_tools() -> list[Tool]:
5e4b8e4… anonymous 47 return [
5e4b8e4… anonymous 48 Tool(
5e4b8e4… anonymous 49 name="ingest_repo",
5e4b8e4… anonymous 50 description="Parse and ingest a local code repository into the navegador graph.",
5e4b8e4… anonymous 51 inputSchema={
5e4b8e4… anonymous 52 "type": "object",
5e4b8e4… anonymous 53 "properties": {
3c74e66… lmata 54 "path": {"type": "string", "description": "Absolute path to the repo."},
3c74e66… lmata 55 "clear": {
3c74e66… lmata 56 "type": "boolean",
3c74e66… lmata 57 "description": "Clear existing graph before ingesting.",
3c74e66… lmata 58 "default": False,
3c74e66… lmata 59 },
5e4b8e4… anonymous 60 },
5e4b8e4… anonymous 61 "required": ["path"],
5e4b8e4… anonymous 62 },
5e4b8e4… anonymous 63 ),
5e4b8e4… anonymous 64 Tool(
5e4b8e4… anonymous 65 name="load_file_context",
3c74e66… lmata 66 description="Return all symbols (functions, classes, imports) in a file.",
5e4b8e4… anonymous 67 inputSchema={
5e4b8e4… anonymous 68 "type": "object",
5e4b8e4… anonymous 69 "properties": {
3c74e66… lmata 70 "file_path": {
3c74e66… lmata 71 "type": "string",
3c74e66… lmata 72 "description": "Relative file path within the ingested repo.",
3c74e66… lmata 73 },
3c74e66… lmata 74 "format": {
758c6b7… lmata 75 "type": "string",
758c6b7… lmata 76 "enum": ["json", "markdown"],
758c6b7… lmata 77 "default": "markdown",
3c74e66… lmata 78 },
5e4b8e4… anonymous 79 },
5e4b8e4… anonymous 80 "required": ["file_path"],
5e4b8e4… anonymous 81 },
5e4b8e4… anonymous 82 ),
5e4b8e4… anonymous 83 Tool(
5e4b8e4… anonymous 84 name="load_function_context",
5e4b8e4… anonymous 85 description="Return context for a function — what it calls and what calls it.",
5e4b8e4… anonymous 86 inputSchema={
5e4b8e4… anonymous 87 "type": "object",
5e4b8e4… anonymous 88 "properties": {
5e4b8e4… anonymous 89 "name": {"type": "string", "description": "Function name."},
5e4b8e4… anonymous 90 "file_path": {"type": "string", "description": "Relative file path."},
5e4b8e4… anonymous 91 "depth": {"type": "integer", "default": 2},
3c74e66… lmata 92 "format": {
758c6b7… lmata 93 "type": "string",
758c6b7… lmata 94 "enum": ["json", "markdown"],
758c6b7… lmata 95 "default": "markdown",
3c74e66… lmata 96 },
5e4b8e4… anonymous 97 },
5e4b8e4… anonymous 98 "required": ["name", "file_path"],
5e4b8e4… anonymous 99 },
5e4b8e4… anonymous 100 ),
5e4b8e4… anonymous 101 Tool(
5e4b8e4… anonymous 102 name="load_class_context",
5e4b8e4… anonymous 103 description="Return context for a class — methods, inheritance, subclasses.",
5e4b8e4… anonymous 104 inputSchema={
5e4b8e4… anonymous 105 "type": "object",
5e4b8e4… anonymous 106 "properties": {
5e4b8e4… anonymous 107 "name": {"type": "string", "description": "Class name."},
5e4b8e4… anonymous 108 "file_path": {"type": "string", "description": "Relative file path."},
3c74e66… lmata 109 "format": {
758c6b7… lmata 110 "type": "string",
758c6b7… lmata 111 "enum": ["json", "markdown"],
758c6b7… lmata 112 "default": "markdown",
3c74e66… lmata 113 },
5e4b8e4… anonymous 114 },
5e4b8e4… anonymous 115 "required": ["name", "file_path"],
5e4b8e4… anonymous 116 },
5e4b8e4… anonymous 117 ),
5e4b8e4… anonymous 118 Tool(
5e4b8e4… anonymous 119 name="search_symbols",
5e4b8e4… anonymous 120 description="Fuzzy search for functions, classes, or methods by name.",
5e4b8e4… anonymous 121 inputSchema={
5e4b8e4… anonymous 122 "type": "object",
5e4b8e4… anonymous 123 "properties": {
5e4b8e4… anonymous 124 "query": {"type": "string", "description": "Partial name to search."},
5e4b8e4… anonymous 125 "limit": {"type": "integer", "default": 20},
5e4b8e4… anonymous 126 },
5e4b8e4… anonymous 127 "required": ["query"],
5e4b8e4… anonymous 128 },
5e4b8e4… anonymous 129 ),
5e4b8e4… anonymous 130 Tool(
5e4b8e4… anonymous 131 name="query_graph",
5e4b8e4… anonymous 132 description="Execute a raw Cypher query against the navegador graph.",
5e4b8e4… anonymous 133 inputSchema={
5e4b8e4… anonymous 134 "type": "object",
5e4b8e4… anonymous 135 "properties": {
5e4b8e4… anonymous 136 "cypher": {"type": "string", "description": "Cypher query string."},
5e4b8e4… anonymous 137 },
5e4b8e4… anonymous 138 "required": ["cypher"],
5e4b8e4… anonymous 139 },
5e4b8e4… anonymous 140 ),
5e4b8e4… anonymous 141 Tool(
5e4b8e4… anonymous 142 name="graph_stats",
5e4b8e4… anonymous 143 description="Return node and edge counts for the current graph.",
5e4b8e4… anonymous 144 inputSchema={"type": "object", "properties": {}},
5e4b8e4… anonymous 145 ),
ece88ca… lmata 146 Tool(
ece88ca… lmata 147 name="get_rationale",
e5e1a3b… lmata 148 description="Return rationale, alternatives, and status of a decision.",
ece88ca… lmata 149 inputSchema={
ece88ca… lmata 150 "type": "object",
ece88ca… lmata 151 "properties": {
ece88ca… lmata 152 "name": {"type": "string", "description": "Decision name."},
ece88ca… lmata 153 "format": {
ece88ca… lmata 154 "type": "string",
ece88ca… lmata 155 "enum": ["json", "markdown"],
ece88ca… lmata 156 "default": "markdown",
ece88ca… lmata 157 },
ece88ca… lmata 158 },
ece88ca… lmata 159 "required": ["name"],
ece88ca… lmata 160 },
ece88ca… lmata 161 ),
ece88ca… lmata 162 Tool(
ece88ca… lmata 163 name="find_owners",
ece88ca… lmata 164 description="Find people (owners, stakeholders) assigned to a node.",
ece88ca… lmata 165 inputSchema={
ece88ca… lmata 166 "type": "object",
ece88ca… lmata 167 "properties": {
ece88ca… lmata 168 "name": {"type": "string", "description": "Node name."},
ece88ca… lmata 169 "file_path": {
ece88ca… lmata 170 "type": "string",
ece88ca… lmata 171 "description": "Narrow to a specific file.",
ece88ca… lmata 172 "default": "",
ece88ca… lmata 173 },
ece88ca… lmata 174 },
ece88ca… lmata 175 "required": ["name"],
ece88ca… lmata 176 },
ece88ca… lmata 177 ),
ece88ca… lmata 178 Tool(
ece88ca… lmata 179 name="search_knowledge",
e5e1a3b… lmata 180 description="Search concepts, rules, decisions, and wiki pages.",
ece88ca… lmata 181 inputSchema={
ece88ca… lmata 182 "type": "object",
ece88ca… lmata 183 "properties": {
ece88ca… lmata 184 "query": {"type": "string", "description": "Search query."},
ece88ca… lmata 185 "limit": {"type": "integer", "default": 20},
ece88ca… lmata 186 },
ece88ca… lmata 187 "required": ["query"],
1ceb8b0… lmata 188 },
1ceb8b0… lmata 189 ),
1ceb8b0… lmata 190 Tool(
1ceb8b0… lmata 191 name="blast_radius",
1ceb8b0… lmata 192 description=(
1ceb8b0… lmata 193 "Impact analysis: find all nodes and files affected by changing a symbol. "
1ceb8b0… lmata 194 "Traverses CALLS, REFERENCES, INHERITS, IMPLEMENTS, ANNOTATES edges outward."
1ceb8b0… lmata 195 ),
1ceb8b0… lmata 196 inputSchema={
1ceb8b0… lmata 197 "type": "object",
1ceb8b0… lmata 198 "properties": {
1ceb8b0… lmata 199 "name": {"type": "string", "description": "Symbol name to analyse."},
1ceb8b0… lmata 200 "file_path": {
1ceb8b0… lmata 201 "type": "string",
1ceb8b0… lmata 202 "description": "Narrow to a specific file (optional).",
1ceb8b0… lmata 203 "default": "",
1ceb8b0… lmata 204 },
1ceb8b0… lmata 205 "depth": {
1ceb8b0… lmata 206 "type": "integer",
1ceb8b0… lmata 207 "description": "Maximum traversal depth.",
1ceb8b0… lmata 208 "default": 3,
1ceb8b0… lmata 209 },
1ceb8b0… lmata 210 },
1ceb8b0… lmata 211 "required": ["name"],
b3b8664… lmata 212 },
b3b8664… lmata 213 ),
5e4b8e4… anonymous 214 ]
5e4b8e4… anonymous 215
5e4b8e4… anonymous 216 @server.call_tool()
5e4b8e4… anonymous 217 async def call_tool(name: str, arguments: dict) -> list[TextContent]:
5e4b8e4… anonymous 218 loader = _get_loader()
5e4b8e4… anonymous 219
5e4b8e4… anonymous 220 if name == "ingest_repo":
b3b8664… lmata 221 if read_only:
e5e1a3b… lmata 222 return [
e5e1a3b… lmata 223 TextContent(
e5e1a3b… lmata 224 type="text",
e5e1a3b… lmata 225 text="Error: ingest_repo is disabled in read-only mode.",
e5e1a3b… lmata 226 )
e5e1a3b… lmata 227 ]
5e4b8e4… anonymous 228 from navegador.ingestion import RepoIngester
758c6b7… lmata 229
5e4b8e4… anonymous 230 ingester = RepoIngester(loader.store)
5e4b8e4… anonymous 231 stats = ingester.ingest(arguments["path"], clear=arguments.get("clear", False))
5e4b8e4… anonymous 232 return [TextContent(type="text", text=json.dumps(stats, indent=2))]
5e4b8e4… anonymous 233
5e4b8e4… anonymous 234 elif name == "load_file_context":
5e4b8e4… anonymous 235 bundle = loader.load_file(arguments["file_path"])
5e4b8e4… anonymous 236 fmt = arguments.get("format", "markdown")
5e4b8e4… anonymous 237 text = bundle.to_markdown() if fmt == "markdown" else bundle.to_json()
5e4b8e4… anonymous 238 return [TextContent(type="text", text=text)]
5e4b8e4… anonymous 239
5e4b8e4… anonymous 240 elif name == "load_function_context":
5e4b8e4… anonymous 241 bundle = loader.load_function(
5e4b8e4… anonymous 242 arguments["name"],
5e4b8e4… anonymous 243 arguments["file_path"],
5e4b8e4… anonymous 244 depth=arguments.get("depth", 2),
5e4b8e4… anonymous 245 )
5e4b8e4… anonymous 246 fmt = arguments.get("format", "markdown")
5e4b8e4… anonymous 247 text = bundle.to_markdown() if fmt == "markdown" else bundle.to_json()
5e4b8e4… anonymous 248 return [TextContent(type="text", text=text)]
5e4b8e4… anonymous 249
5e4b8e4… anonymous 250 elif name == "load_class_context":
5e4b8e4… anonymous 251 bundle = loader.load_class(arguments["name"], arguments["file_path"])
5e4b8e4… anonymous 252 fmt = arguments.get("format", "markdown")
5e4b8e4… anonymous 253 text = bundle.to_markdown() if fmt == "markdown" else bundle.to_json()
5e4b8e4… anonymous 254 return [TextContent(type="text", text=text)]
5e4b8e4… anonymous 255
5e4b8e4… anonymous 256 elif name == "search_symbols":
5e4b8e4… anonymous 257 results = loader.search(arguments["query"], limit=arguments.get("limit", 20))
5e4b8e4… anonymous 258 lines = [f"- **{r.type}** `{r.name}` — `{r.file_path}`:{r.line_start}" for r in results]
5e4b8e4… anonymous 259 return [TextContent(type="text", text="\n".join(lines) or "No results.")]
5e4b8e4… anonymous 260
5e4b8e4… anonymous 261 elif name == "query_graph":
b3b8664… lmata 262 cypher = arguments["cypher"]
b3b8664… lmata 263 if read_only:
b3b8664… lmata 264 try:
b3b8664… lmata 265 validate_cypher(cypher)
b3b8664… lmata 266 except Exception as exc:
b3b8664… lmata 267 return [TextContent(type="text", text=f"Error: {exc}")]
b3b8664… lmata 268 try:
b3b8664… lmata 269 check_complexity(cypher)
b3b8664… lmata 270 except Exception as exc:
b3b8664… lmata 271 return [TextContent(type="text", text=f"Error: {exc}")]
b3b8664… lmata 272 result = loader.store.query(cypher)
5e4b8e4… anonymous 273 rows = result.result_set or []
5e4b8e4… anonymous 274 text = json.dumps(rows, default=str, indent=2)
5e4b8e4… anonymous 275 return [TextContent(type="text", text=text)]
5e4b8e4… anonymous 276
5e4b8e4… anonymous 277 elif name == "graph_stats":
5e4b8e4… anonymous 278 stats = {
5e4b8e4… anonymous 279 "nodes": loader.store.node_count(),
5e4b8e4… anonymous 280 "edges": loader.store.edge_count(),
5e4b8e4… anonymous 281 }
5e4b8e4… anonymous 282 return [TextContent(type="text", text=json.dumps(stats, indent=2))]
ece88ca… lmata 283
ece88ca… lmata 284 elif name == "get_rationale":
ece88ca… lmata 285 bundle = loader.load_decision(arguments["name"])
ece88ca… lmata 286 fmt = arguments.get("format", "markdown")
ece88ca… lmata 287 text = bundle.to_markdown() if fmt == "markdown" else bundle.to_json()
ece88ca… lmata 288 return [TextContent(type="text", text=text)]
ece88ca… lmata 289
ece88ca… lmata 290 elif name == "find_owners":
ece88ca… lmata 291 results = loader.find_owners(
ece88ca… lmata 292 arguments["name"], file_path=arguments.get("file_path", "")
ece88ca… lmata 293 )
ece88ca… lmata 294 if not results:
ece88ca… lmata 295 return [TextContent(type="text", text="No owners found.")]
e5e1a3b… lmata 296 lines = [f"- **{r.name}** ({r.description})" for r in results]
ece88ca… lmata 297 return [TextContent(type="text", text="\n".join(lines))]
ece88ca… lmata 298
ece88ca… lmata 299 elif name == "search_knowledge":
e5e1a3b… lmata 300 results = loader.search_knowledge(arguments["query"], limit=arguments.get("limit", 20))
ece88ca… lmata 301 if not results:
ece88ca… lmata 302 return [TextContent(type="text", text="No results.")]
e5e1a3b… lmata 303 lines = [f"- **{r.type}** `{r.name}` — {r.description or ''}" for r in results]
ece88ca… lmata 304 return [TextContent(type="text", text="\n".join(lines))]
1ceb8b0… lmata 305
1ceb8b0… lmata 306 elif name == "blast_radius":
1ceb8b0… lmata 307 from navegador.analysis.impact import ImpactAnalyzer
1ceb8b0… lmata 308
1ceb8b0… lmata 309 result = ImpactAnalyzer(loader.store).blast_radius(
1ceb8b0… lmata 310 arguments["name"],
1ceb8b0… lmata 311 file_path=arguments.get("file_path", ""),
1ceb8b0… lmata 312 depth=arguments.get("depth", 3),
1ceb8b0… lmata 313 )
1ceb8b0… lmata 314 return [TextContent(type="text", text=json.dumps(result.to_dict(), indent=2))]
5e4b8e4… anonymous 315
5e4b8e4… anonymous 316 return [TextContent(type="text", text=f"Unknown tool: {name}")]
5e4b8e4… anonymous 317
5e4b8e4… anonymous 318 return server

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button