Skip to content

feat(mcp): Graph-aware MCP tools (graph_explore, graph_impact) #208

Closed

Description

@hurttlocker

Summary

Extend Cortex's MCP server with two new tools — graph_explore and graph_impact — so any connected AI agent (Claude Code, Cursor, OpenClaw, etc.) can programmatically traverse the knowledge graph and run impact analysis without needing the web UI or raw API calls.

Parent epic: #204
Depends on: #206 (Impact endpoint)

Background

Cortex's MCP server (cortex mcp) currently exposes a single search tool. This covers keyword/semantic lookup but gives agents no way to:

  1. Explore the graph — "What facts are connected to fact #1234 within 2 hops?"
  2. Analyze impact — "What do I know about 'trading' and how is it all connected?"
  3. Discover clusters — "What are the main topic areas in my knowledge base?"

GitNexus exposes 7 MCP tools for its code graph. We don't need all of them, but graph_explore and graph_impact fill the biggest gaps.

Implementation Plan

1. graph_explore tool

Purpose: Given a starting point (fact ID or subject), return the local subgraph within N hops.

MCP Tool Definition:

{
  "name": "graph_explore",
  "description": "Explore the knowledge graph around a starting point. Returns connected facts within the specified depth. Use to understand relationships between concepts, trace how topics connect, or discover unknown connections.",
  "inputSchema": {
    "type": "object",
    "properties": {
      "subject": {
        "type": "string",
        "description": "Subject to explore from (e.g., 'trading', 'spear'). Mutually exclusive with fact_id."
      },
      "fact_id": {
        "type": "integer",
        "description": "Fact ID to explore from. Mutually exclusive with subject."
      },
      "depth": {
        "type": "integer",
        "description": "How many hops to traverse (1-5, default: 2)",
        "default": 2,
        "minimum": 1,
        "maximum": 5
      },
      "min_confidence": {
        "type": "number",
        "description": "Minimum fact confidence to include (0.0-1.0, default: 0.3)",
        "default": 0.3
      },
      "limit": {
        "type": "integer",
        "description": "Maximum facts to return (default: 50, max: 200)",
        "default": 50,
        "maximum": 200
      },
      "source": {
        "type": "string",
        "description": "Filter by source prefix (e.g., 'github', 'memory/')"
      }
    },
    "oneOf": [
      {"required": ["subject"]},
      {"required": ["fact_id"]}
    ]
  }
}

Response format:

{
  "root": "trading",
  "depth": 2,
  "facts": [
    {
      "id": 1234,
      "subject": "trading",
      "predicate": "uses strategy",
      "object": "ORB (Opening Range Breakout)",
      "confidence": 0.92,
      "hop": 0,
      "source": "memory/2026-02-09.md"
    },
    {
      "id": 1456,
      "subject": "ORB strategy",
      "predicate": "runs on",
      "object": "Alpaca paper account",
      "confidence": 0.88,
      "hop": 1,
      "source": "memory/2026-02-09.md"
    }
  ],
  "edges": [
    {"from": 1234, "to": 1456, "type": "subject_object", "confidence": 0.88}
  ],
  "connected_subjects": ["orb strategy", "alpaca", "options", "public.com"],
  "total_facts": 47
}

2. graph_impact tool

Purpose: Run impact/blast-radius analysis for a subject. Returns facts grouped by relationship type with confidence stats. Wraps the /api/impact endpoint from #206.

MCP Tool Definition:

{
  "name": "graph_impact",
  "description": "Analyze the impact/blast radius of a subject in the knowledge graph. Returns all connected facts grouped by relationship type with confidence statistics. Use to understand the full scope of a topic, find what depends on what, or assess knowledge coverage.",
  "inputSchema": {
    "type": "object",
    "required": ["subject"],
    "properties": {
      "subject": {
        "type": "string",
        "description": "Subject to analyze (e.g., 'trading', 'spear', 'Q')"
      },
      "depth": {
        "type": "integer",
        "description": "Traversal depth (1-5, default: 3)",
        "default": 3,
        "minimum": 1,
        "maximum": 5
      },
      "min_confidence": {
        "type": "number",
        "description": "Minimum confidence threshold (0.0-1.0, default: 0.3)",
        "default": 0.3
      }
    }
  }
}

Response format: Same as /api/impact response (see #206).

3. list_clusters tool (bonus, lightweight)

{
  "name": "list_clusters",
  "description": "List all topic clusters in the knowledge graph with cohesion scores and fact counts. Use to discover the main topic areas in the knowledge base.",
  "inputSchema": {
    "type": "object",
    "properties": {}
  }
}

Depends on: #207 (Topic clustering). If clustering isn't merged yet, this tool can be gated behind a feature flag.

4. MCP Resources (read-only context)

Add two new MCP resources:

URI Description
cortex://graph/subjects List of all unique subjects with fact counts
cortex://graph/clusters All clusters with cohesion scores (if clustering is available)

These let agents discover what's in the graph without running tools.

5. Integration in internal/mcp/server.go

  • Register graph_explore and graph_impact alongside existing search tool
  • Both tools reuse existing store.TraverseGraph() and the new impact analysis from feat(graph): Impact / blast-radius endpoint and UI view #206
  • Add list_clusters behind feature flag (checks if clusters table exists)
  • Register new resources

Testing

Test Description
TestMCPGraphExploreBySubject Returns connected facts for a known subject
TestMCPGraphExploreByFactID Returns connected facts starting from a fact ID
TestMCPGraphExploreDepthLimit Depth=1 returns fewer results than depth=3
TestMCPGraphExploreSourceFilter source parameter filters correctly
TestMCPGraphImpactBasic Returns grouped facts with confidence stats
TestMCPGraphImpactUnknownSubject Returns empty result, not error
TestMCPListClusters Returns cluster list when clustering is available
TestMCPListClustersNoTable Returns graceful "not available" when no clustering
TestMCPResourceSubjects Resource returns unique subject list

Acceptance Criteria

  • graph_explore tool registered in MCP server and callable
  • graph_impact tool registered and returns grouped results
  • list_clusters available when clustering table exists, gracefully absent otherwise
  • cortex://graph/subjects resource returns subject list
  • Tools work via both stdio and HTTP+SSE MCP transports
  • 9+ new tests passing
  • Existing search tool unaffected

Files to Create/Modify

File Change
internal/mcp/graph_tools.go New — graph_explore and graph_impact tool handlers
internal/mcp/graph_tools_test.go New — tests
internal/mcp/server.go Register new tools + resources
internal/mcp/resources.go Add cortex://graph/subjects and cortex://graph/clusters
README.md Document new MCP tools

Estimated Effort

Medium — handlers are thin wrappers around existing traversal + the new impact endpoint (#206). Main work is tool schema design, response formatting, and tests.

Activity

  1. added
    enhancement
    v0.8.0
    mcp
    on Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementmcpv0.8.0

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions