Querying the Knowledge Graph
Programmatic access via CLI, REST API, and MCP.
Overview
Three ways to query:
| Method | Best For | Authentication |
|---|---|---|
| CLI | Interactive use, scripts | OAuth (browser login) |
| REST API | Custom applications | OAuth tokens |
| MCP | AI assistants (Claude, etc.) | Configured per-assistant |
CLI Queries
The kg command-line interface provides full access.
Authentication
# Login (opens browser for OAuth)
kg login
# Check configuration and auth status
kg config show
# Logout
kg logout
Search
# Basic search
kg search "your query"
# With options
kg search --limit 20 --ontology "research" "machine learning"
# Output formats
kg search --format json "query"
kg search --format table "query"
Concept Operations
# Get full details
kg search details <concept-id>
# Find related concepts
kg search related <concept-id>
kg search related <concept-id> --depth 2 --type SUPPORTS
# Find paths between concepts
kg search connect "concept A" "concept B"
kg search connect <id1> <id2> --max-hops 4
Ontology Operations
# List ontologies
kg ontology list
# Get ontology info
kg ontology info <name>
# List files in ontology
kg ontology files <name>
Job Management
# List jobs
kg job list
kg job list --status pending
# Check job status
kg job status <job-id>
# Approve pending job
kg job approve <job-id>
# Cancel job
kg job cancel <job-id>
REST API
Direct HTTP access for custom applications.
Authentication
Get an OAuth token first:
# Using the CLI token
TOKEN=$(kg auth token)
# Or via OAuth flow
curl -X POST "http://localhost:8000/auth/oauth/token" \
-d "grant_type=authorization_code&code=..."
Search Endpoint
curl "http://localhost:8000/concepts/search?query=climate+change&limit=10" \
-H "Authorization: Bearer $TOKEN"
Response:
{
"results": [
{
"concept_id": "abc123",
"name": "Climate change increases extreme weather",
"similarity": 0.89,
"grounding_strength": 0.72,
"source_count": 5
}
]
}
Concept Details
Response:
{
"concept_id": "abc123",
"name": "Climate change increases extreme weather",
"type": "claim",
"grounding_strength": 0.72,
"evidence": [
{
"source_id": "src456",
"text": "Studies show that climate change...",
"document": "IPCC Report 2023"
}
],
"relationships": [
{
"type": "CAUSES",
"target_id": "def789",
"target_name": "Increased flooding"
}
]
}
Ingest Document
curl -X POST "http://localhost:8000/ingest" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: multipart/form-data" \
-F "file=@document.pdf" \
-F "ontology=research" \
-F "auto_approve=true"
Full API Reference
See API Reference for complete endpoint documentation.
MCP (Model Context Protocol)
For AI assistants like Claude Desktop.
Setup
Add to your Claude Desktop config (~/.config/claude/claude_desktop_config.json):
{
"mcpServers": {
"knowledge-graph": {
"command": "node",
"args": ["/path/to/knowledge-graph-system/mcp/dist/index.js"],
"env": {
"KG_API_URL": "http://localhost:8000"
}
}
}
}
Available Tools
Once configured, Claude can use these tools:
search
Find concepts by semantic similarity.
Parameters:
- query (required): Search text
- limit: Max results (default 10)
- min_similarity: Threshold 0-1 (default 0.7)
- ontology: Filter by ontology name
concept
Work with specific concepts.
Actions:
- details: Get full concept with evidence
- related: Find connected concepts
- connect: Find paths between concepts
ingest
Add documents to the knowledge graph.
ontology
Manage knowledge collections.
source
Retrieve original source text.
MCP for AI Reasoning
When an AI uses MCP, it can:
- Query for context: Before answering, search for relevant concepts
- Check grounding: Verify claims have evidence
- Find contradictions: Identify where sources disagree
- Trace sources: Link answers to original documents
- Build knowledge: Ingest new information during conversation
Example AI workflow:
User: "What are the effects of sleep deprivation?"
AI thinks: Let me check the knowledge graph...
[Uses search tool: "sleep deprivation effects"]
AI: Based on the knowledge graph, sleep deprivation has several documented effects:
- Memory impairment (grounding: 0.85, 12 sources)
- Reduced cognitive function (grounding: 0.78, 8 sources)
- Increased accident risk (grounding: 0.65, 5 sources)
Sources include: Smith et al. 2023, Sleep Research Journal...
Full MCP Reference
See MCP Reference for complete tool documentation.
Query Patterns
Get Grounded Answers
- Search for the topic
- Check grounding scores
- For high-grounding concepts, trust the answer
- For low-grounding, caveat with uncertainty
Explore a Topic
- Search for central concept
- Get related concepts (depth 2)
- Look for clusters of connected ideas
- Follow interesting paths
Verify a Claim
- Search for the specific claim
- Check if concept exists
- If yes, check grounding and sources
- If no, the claim isn't in your knowledge base
Find Disagreements
- Search for contested topic
- Look for concepts with grounding near 0
- Get details to see conflicting sources
- Use
connectto find the conflict structure
Tips
Use Semantic Queries
"What causes inflation" works better than "inflation causes" because the system matches by meaning.
Check Multiple Phrasings
If initial search misses, try synonyms or related terms.
Follow the Evidence
Always check source text for important claims. Grounding tells you confidence, sources tell you why.
Combine Methods
Use CLI for exploration, API for automation, MCP for AI-assisted work.
Next Steps
- Understanding Grounding - Interpret confidence scores
- API Reference - Complete endpoint docs
- MCP Reference - All MCP tools