# githubtarohario/mkgr-agent architecture

> Architecture diagram of the GitHub repository githubtarohario/mkgr-agent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/githubtarohario/mkgr-agent
- Repository: https://github.com/githubtarohario/mkgr-agent

## Overview

#### Purpose and Entry Points

MKGR-Agent is a CLI medical knowledge-graph research MVP for Disease → Phenotype → Gene exploration. The primary entry point is `app/main.py`; it builds local RDF assets with `--build-rdf` or runs a natural-language research query. `app/orchestrator.py` coordinates the agent pipeline and produces structured answers plus GraphML.

#### Knowledge Ingestion and RDF Boundary

Ontology/source data from MONDO, HPO, and HGNC is parsed and integrated into RDF by the sole write path, `app/graph/build_rdf.py`. Source-specific parsing is isolated in `app/graph/obo_parser.py`, while namespaces and RDF construction conventions reside in `app/graph/ns.py`. This is the critical offline-build/runtime boundary: generated Turtle/N-Triples and Oxigraph data are consumed read-only during queries.

#### Graph Storage, Querying, and Projection

`app/graph/store.py` opens the persisted RDF graph for runtime access. `app/tools/sparql.py` is the query-facing abstraction used by agents rather than allowing arbitrary graph mutation. RDF can be projected to NetworkX and narrowed to relevant neighborhoods through `app/graph/rdf_to_networkx.py` and `app/graph/subgraph.py`; `app/graph/graphml_export.py` exports the resulting research graph for Gephi.

#### Query Resolution and Entity Validation

User requests first pass through `app/agents/query_parser.py`, which extracts disease candidates with rules, Japanese aliases, and fuzzy matching. `app/tools/ja_alias.py` supplies the limited Japanese disease alias vocabulary, and `app/tools/mondo.py` resolves candidates against MONDO. This validation boundary prevents ungrounded disease names from reaching graph traversal.

#### Three-Agent Research Pipeline

The orchestrator invokes:
- Knowledge retrieval in `app/agents/knowledge_agent.py`, which explores RDF/SPARQL relations for disease, phenotype, and gene entities.
- Context retrieval in `app/agents/literature_agent.py`, which currently searches RDF-derived local text rather than external literature.
- Relation checking and evidence labeling in `app/agents/critic_agent.py`, which revalidates claims against RDF and caps evidence at ontology-derived E2.

Shared result structures are represented by `app/models/entity.py`, `app/models/relation.py`, and `app/models/evidence.py`.

#### Retrieval and External-Service Boundaries

“Literature” retrieval is local LightRAG-lite TF-IDF search via `app/tools/lightrag_tool.py` and `app/tools/search_index.py`; PubMed/PMC is not integrated. Optional OpenAI access is isolated in `app/tools/llm_client.py`: it only helps extract a disease-name string when rule extraction is weak, after which MONDO fuzzy validation remains mandatory. Runtime otherwise operates locally on Python 3.11, RDF/Oxigraph, and graph-export tooling.

#### Domain Scope and Reliability Constraints

The implemented graph intentionally limits disease–gene links to MONDO germline-mutation relations; it does not infer links by joining disease and gene OMIM cross-references. HPO annotations inherit rare-disease source coverage. Consequently, an empty phenotype or gene result can be a valid source-coverage outcome, and agents should preserve that distinction rather than fabricate relationships.

Main entry points: [`app/orchestrator.py`](https://github.com/githubtarohario/mkgr-agent/blob/master/app/orchestrator.py) (Research orchestrator), [`app/graph/build_rdf.py`](https://github.com/githubtarohario/mkgr-agent/blob/master/app/graph/build_rdf.py) (RDF builder), [`app/agents/query_parser.py`](https://github.com/githubtarohario/mkgr-agent/blob/master/app/agents/query_parser.py) (Disease query parser), [`app/agents/knowledge_agent.py`](https://github.com/githubtarohario/mkgr-agent/blob/master/app/agents/knowledge_agent.py) (Knowledge agent)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["CLI &amp; Orchestration"]
  node_cli["CLI entry point<br/>Python CLI<br/>[main.py]"]
  node_orchestrator["Research orchestrator<br/>pipeline coordinator<br/>[orchestrator.py]"]
end

subgraph group_rdf["RDF Build &amp; Graph Data"]
  node_rdf_builder["RDF builder<br/>offline write path<br/>[build_rdf.py]"]
  node_obo_parser["Ontology parser<br/>source parser<br/>[obo_parser.py]"]
  node_namespaces["RDF namespaces<br/>RDF conventions<br/>[ns.py]"]
  node_rdf_assets[("Persisted RDF assets<br/>Turtle, N-Triples, Oxigraph")]
  node_graph_store[("Runtime graph store<br/>Oxigraph access<br/>[store.py]")]
  node_sparql["SPARQL query tool<br/>query abstraction<br/>[sparql.py]"]
end

subgraph group_resolution["Query Resolution"]
  node_query_parser["Disease query parser<br/>candidate extraction<br/>[query_parser.py]"]
  node_ja_alias["Japanese aliases<br/>alias vocabulary<br/>[ja_alias.py]"]
  node_mondo_resolver["MONDO resolver<br/>entity validation<br/>[mondo.py]"]
  node_llm_client["Optional OpenAI helper<br/>LLM boundary<br/>[llm_client.py]"]
end

subgraph group_agents["Research Agents"]
  node_knowledge_agent["Knowledge agent<br/>RDF relation retrieval<br/>[knowledge_agent.py]"]
  node_literature_agent["Context agent<br/>local text retrieval"]
  node_critic_agent["Critic agent<br/>claim validation<br/>[critic_agent.py]"]
  node_local_search["LightRAG-lite search<br/>TF-IDF retrieval<br/>[lightrag_tool.py]"]
  node_search_index[("Local search index<br/>TF-IDF index<br/>[search_index.py]")]
end

subgraph group_outputs["Results &amp; Export"]
  node_result_models["Research result models<br/>entity, relation, evidence<br/>[entity.py]"]
  node_subgraph_projection["Research subgraph<br/>NetworkX projection<br/>[subgraph.py]"]
  node_graphml_export["GraphML export<br/>Gephi output<br/>[graphml_export.py]"]
end

node_cli -->|"--build-rdf"| node_rdf_builder
node_cli -->|"research query"| node_orchestrator
node_rdf_builder -->|"parses MONDO, HPO, HGNC"| node_obo_parser
node_rdf_builder -->|"uses RDF conventions"| node_namespaces
node_rdf_builder -->|"writes"| node_rdf_assets
node_rdf_assets -->|"read-only runtime data"| node_graph_store
node_graph_store -->|"serves queries"| node_sparql
node_orchestrator -->|"natural-language request"| node_query_parser
node_query_parser -->|"looks up aliases"| node_ja_alias
node_query_parser -.->|"fallback extraction"| node_llm_client
node_query_parser -->|"candidate validation"| node_mondo_resolver
node_mondo_resolver -->|"grounded disease"| node_knowledge_agent
node_orchestrator -->|"invokes"| node_knowledge_agent
node_orchestrator -->|"invokes"| node_literature_agent
node_orchestrator -->|"invokes"| node_critic_agent
node_knowledge_agent -->|"retrieves relations"| node_sparql
node_literature_agent -->|"retrieves context"| node_local_search
node_local_search -->|"searches"| node_search_index
node_critic_agent -->|"revalidates claims"| node_sparql
node_knowledge_agent -->|"produces findings"| node_result_models
node_literature_agent -->|"adds context"| node_result_models
node_critic_agent -->|"labels evidence"| node_result_models
node_sparql -->|"RDF relations"| node_subgraph_projection
node_subgraph_projection -->|"exports"| node_graphml_export
node_orchestrator -->|"produces GraphML"| node_graphml_export

click node_cli "https://github.com/githubtarohario/mkgr-agent/blob/master/app/main.py"
click node_orchestrator "https://github.com/githubtarohario/mkgr-agent/blob/master/app/orchestrator.py"
click node_rdf_builder "https://github.com/githubtarohario/mkgr-agent/blob/master/app/graph/build_rdf.py"
click node_obo_parser "https://github.com/githubtarohario/mkgr-agent/blob/master/app/graph/obo_parser.py"
click node_namespaces "https://github.com/githubtarohario/mkgr-agent/blob/master/app/graph/ns.py"
click node_graph_store "https://github.com/githubtarohario/mkgr-agent/blob/master/app/graph/store.py"
click node_sparql "https://github.com/githubtarohario/mkgr-agent/blob/master/app/tools/sparql.py"
click node_query_parser "https://github.com/githubtarohario/mkgr-agent/blob/master/app/agents/query_parser.py"
click node_ja_alias "https://github.com/githubtarohario/mkgr-agent/blob/master/app/tools/ja_alias.py"
click node_mondo_resolver "https://github.com/githubtarohario/mkgr-agent/blob/master/app/tools/mondo.py"
click node_llm_client "https://github.com/githubtarohario/mkgr-agent/blob/master/app/tools/llm_client.py"
click node_knowledge_agent "https://github.com/githubtarohario/mkgr-agent/blob/master/app/agents/knowledge_agent.py"
click node_literature_agent "https://github.com/githubtarohario/mkgr-agent/blob/master/app/agents/literature_agent.py"
click node_critic_agent "https://github.com/githubtarohario/mkgr-agent/blob/master/app/agents/critic_agent.py"
click node_local_search "https://github.com/githubtarohario/mkgr-agent/blob/master/app/tools/lightrag_tool.py"
click node_search_index "https://github.com/githubtarohario/mkgr-agent/blob/master/app/tools/search_index.py"
click node_result_models "https://github.com/githubtarohario/mkgr-agent/blob/master/app/models/entity.py"
click node_subgraph_projection "https://github.com/githubtarohario/mkgr-agent/blob/master/app/graph/subgraph.py"
click node_graphml_export "https://github.com/githubtarohario/mkgr-agent/blob/master/app/graph/graphml_export.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_orchestrator toneBlue
class node_rdf_builder,node_obo_parser,node_namespaces,node_rdf_assets,node_graph_store,node_sparql toneAmber
class node_query_parser,node_ja_alias,node_mondo_resolver,node_llm_client toneMint
class node_knowledge_agent,node_literature_agent,node_critic_agent,node_local_search,node_search_index toneRose
class node_result_models,node_subgraph_projection,node_graphml_export toneIndigo
```

## Components

### CLI & Orchestration

- [CLI entry point](https://github.com/githubtarohario/mkgr-agent/blob/master/app/main.py): Python CLI · Builds RDF assets or runs a research query. · `app/main.py`
- [Research orchestrator](https://github.com/githubtarohario/mkgr-agent/blob/master/app/orchestrator.py): pipeline coordinator · Coordinates resolution, agents, structured answers, and GraphML. · `app/orchestrator.py`

### RDF Build & Graph Data

Offline-generated RDF is read-only at runtime.

- [RDF builder](https://github.com/githubtarohario/mkgr-agent/blob/master/app/graph/build_rdf.py): offline write path · The sole path that integrates source ontologies into RDF. · `app/graph/build_rdf.py`
- [Ontology parser](https://github.com/githubtarohario/mkgr-agent/blob/master/app/graph/obo_parser.py): source parser · `app/graph/obo_parser.py`
- [RDF namespaces](https://github.com/githubtarohario/mkgr-agent/blob/master/app/graph/ns.py): RDF conventions · `app/graph/ns.py`
- **Persisted RDF assets**: Turtle, N-Triples, Oxigraph · Generated offline and consumed read-only during research.
- [Runtime graph store](https://github.com/githubtarohario/mkgr-agent/blob/master/app/graph/store.py): Oxigraph access · `app/graph/store.py`
- [SPARQL query tool](https://github.com/githubtarohario/mkgr-agent/blob/master/app/tools/sparql.py): query abstraction · Agent-facing RDF access boundary. · `app/tools/sparql.py`

### Query Resolution

- [Disease query parser](https://github.com/githubtarohario/mkgr-agent/blob/master/app/agents/query_parser.py): candidate extraction · Uses rules, Japanese aliases, and fuzzy matching. · `app/agents/query_parser.py`
- [Japanese aliases](https://github.com/githubtarohario/mkgr-agent/blob/master/app/tools/ja_alias.py): alias vocabulary · `app/tools/ja_alias.py`
- [MONDO resolver](https://github.com/githubtarohario/mkgr-agent/blob/master/app/tools/mondo.py): entity validation · Mandatory grounding boundary for disease candidates. · `app/tools/mondo.py`
- [Optional OpenAI helper](https://github.com/githubtarohario/mkgr-agent/blob/master/app/tools/llm_client.py): LLM boundary · Only assists disease-name extraction when rules are weak. · `app/tools/llm_client.py`

### Research Agents

- [Knowledge agent](https://github.com/githubtarohario/mkgr-agent/blob/master/app/agents/knowledge_agent.py): RDF relation retrieval · Explores disease, phenotype, and gene relations. · `app/agents/knowledge_agent.py`
- [Context agent](https://github.com/githubtarohario/mkgr-agent/blob/master/app/agents/literature_agent.py): local text retrieval · Retrieves RDF-derived local context, not PubMed or PMC. · `app/agents/literature_agent.py`
- [Critic agent](https://github.com/githubtarohario/mkgr-agent/blob/master/app/agents/critic_agent.py): claim validation · Revalidates RDF claims and caps evidence at ontology-derived E2. · `app/agents/critic_agent.py`
- [LightRAG-lite search](https://github.com/githubtarohario/mkgr-agent/blob/master/app/tools/lightrag_tool.py): TF-IDF retrieval · `app/tools/lightrag_tool.py`
- [Local search index](https://github.com/githubtarohario/mkgr-agent/blob/master/app/tools/search_index.py): TF-IDF index · `app/tools/search_index.py`

### Results & Export

- [Research result models](https://github.com/githubtarohario/mkgr-agent/blob/master/app/models/entity.py): entity, relation, evidence · `app/models/entity.py`
- [Research subgraph](https://github.com/githubtarohario/mkgr-agent/blob/master/app/graph/subgraph.py): NetworkX projection · Projects RDF and narrows relevant neighborhoods. · `app/graph/subgraph.py`
- [GraphML export](https://github.com/githubtarohario/mkgr-agent/blob/master/app/graph/graphml_export.py): Gephi output · `app/graph/graphml_export.py`

## Connections

- CLI entry point → RDF builder: --build-rdf
- CLI entry point → Research orchestrator: research query
- RDF builder → Ontology parser: parses MONDO, HPO, HGNC
- RDF builder → RDF namespaces: uses RDF conventions
- RDF builder → Persisted RDF assets: writes
- Persisted RDF assets → Runtime graph store: read-only runtime data
- Runtime graph store → SPARQL query tool: serves queries
- Research orchestrator → Disease query parser: natural-language request
- Disease query parser → Japanese aliases: looks up aliases
- Disease query parser → Optional OpenAI helper: fallback extraction
- Disease query parser → MONDO resolver: candidate validation
- MONDO resolver → Knowledge agent: grounded disease
- Research orchestrator → Knowledge agent: invokes
- Research orchestrator → Context agent: invokes
- Research orchestrator → Critic agent: invokes
- Knowledge agent → SPARQL query tool: retrieves relations
- Context agent → LightRAG-lite search: retrieves context
- LightRAG-lite search → Local search index: searches
- Critic agent → SPARQL query tool: revalidates claims
- Knowledge agent → Research result models: produces findings
- Context agent → Research result models: adds context
- Critic agent → Research result models: labels evidence
- SPARQL query tool → Research subgraph: RDF relations
- Research subgraph → GraphML export: exports
- Research orchestrator → GraphML export: produces GraphML
