MCP File Context Tools
Enhanced Model Context Protocol (MCP) tools for file-based context management, providing line-numbered references and semantic code search.
Overview
Terraphim's MCP server exposes powerful tools for working with file context, similar to Conare AI's "@" file referencing but with additional semantic capabilities through knowledge graph integration.
Available Tools
1. extract_paragraphs_from_automata
Extract paragraphs from text starting at matched terms, with line numbers.
Purpose: Get context around specific concepts or code elements with precise line references.
Parameters:
text(string): The text content to searchterm(string): The term to find and extract context aroundmax_paragraphs(number, optional): Maximum paragraphs to return (default: 3)
Returns:
Example Usage:
// Via MCP in Claude Desktop
const result = await mcp.callTool("extract_paragraphs_from_automata", {
text: fileContents,
term: "async fn",
max_paragraphs: 2
});
// Returns paragraphs containing "async fn" with line numbersUse Cases:
- Extract function definitions with line references
- Get context around specific patterns or concepts
- Reference code snippets in documentation with accurate line numbers
2. search (Enhanced with File Context)
Search knowledge graph with document content and line numbers.
Purpose: Semantic search that returns full document context with file paths.
Parameters:
query(string): Search term or phraserole(string, optional): Role context for searchlimit(number, optional): Maximum results (default: 10)skip(number, optional): Results to skip for pagination
Returns:
Enhanced Features:
- Returns
urlfield with file paths - Includes full
bodycontent for local files - Provides
rankfor relevance sorting - Adds
line_countfor context size estimation
Example Usage:
const results = await mcp.callTool("search", {
query: "async cancellation pattern",
role: "Context Engineer",
limit: 5
});
// Returns documents about async cancellation with file paths
// Claude can then extract specific lines or functions3. autocomplete_terms
Autocomplete search terms from knowledge graph.
Purpose: Discover related concepts and get term suggestions.
Parameters:
query(string): Prefix or term to autocompletelimit(number, optional): Maximum suggestions (default: 10)role(string, optional): Role context
Returns:
Use Cases:
- Discover related concepts while typing
- Find synonyms and related terms
- Navigate knowledge graph interactively
4. autocomplete_with_snippets
Autocomplete with code/documentation snippets.
Purpose: Get term suggestions with preview snippets.
Parameters:
query(string): Search prefixlimit(number, optional): Maximum resultsrole(string, optional): Role context
Returns:
Use Cases:
- Preview code patterns before inserting
- See usage examples during autocomplete
- Learn API signatures interactively
5. find_matches
Find all concept matches in text with positions.
Purpose: Identify concepts/terms in code or documentation.
Parameters:
text(string): Text to analyzerole(string, optional): Role for knowledge graph context
Returns:
Use Cases:
- Analyze code for known patterns
- Tag documentation with concepts
- Build code-to-concept mappings
6. is_all_terms_connected_by_path
Check if terms are related in the knowledge graph.
Purpose: Verify semantic relationships between concepts.
Parameters:
terms(array of strings): Terms to check connectivity
Returns:
Use Cases:
- Verify that code uses related concepts
- Find semantic gaps in documentation
- Validate tag consistency
Workflow Examples
Example 1: Find and Reference Code Pattern
User: "Show me how to handle async cancellation in Rust"
Claude's Workflow:
// 1. Search for relevant documents
const searchResults = await mcp.callTool("search", {
query: "async cancellation",
role: "Context Engineer",
limit: 3
});
// 2. Extract specific pattern with line numbers
const doc = searchResults.documents[0];
const paragraphs = await mcp.callTool("extract_paragraphs_from_automata", {
text: doc.body,
term: "tokio::select",
max_paragraphs: 1
});
// 3. Return with precise reference
console.log(`Found pattern at ${doc.url}:${paragraphs[0].start_line}`);Claude's Response:
Here's the recommended async cancellation pattern from
docs/vibe-rules/rust/async-patterns.md:42-48:
select! Example 2: Interactive Code Completion
User: Typing "async" in editor
Claude's Workflow:
// 1. Get autocomplete suggestions with snippets
const suggestions = await mcp.callTool("autocomplete_with_snippets", {
query: "async",
limit: 5
});
// 2. Show suggestions to user
// User selects "async-cancellation"
// 3. Get full context
const context = await mcp.callTool("search", {
query: "async cancellation pattern",
limit: 1
});Result: Full pattern with explanation inserted into editor.
Example 3: Code Review with Concept Analysis
User: "Review this code for async best practices"
Claude's Workflow:
// 1. Find all async-related concepts in code
const matches = await mcp.callTool("find_matches", {
text: userCode,
role: "Context Engineer"
});
// 2. For each match, check if it follows patterns
for (const match of matches.matches) {
// 3. Search for related best practices
const practices = await mcp.callTool("search", {
query: match.term,
role: "Context Engineer"
});
// 4. Compare code to best practice
// Report violations or confirm compliance
}Claude's Response:
I found 3 async patterns in your code:
- Line 42:
tokio::spawn- ✅ Follows best practice (seeasync-patterns.md:15)- Line 67: Unbounded channel - ⚠️ Consider using bounded channel (see
async-patterns.md:45)- Line 89: No cancellation handling - ❌ Missing cancellation (see
async-patterns.md:78)
Implementation Details
Line Number Tracking
Line numbers are calculated during paragraph extraction:
File Path Resolution
Documents include url field with file:// URLs:
URLs are resolved to absolute paths:
- Local files:
file:///absolute/path/to/file.rs - Remote URLs:
https://example.com/doc.html - Relative paths: Resolved relative to workspace root
Configuration
Enable file context tools in MCP server:
Role Configuration
Configure Context Engineer role with appropriate haystacks:
Performance Considerations
Caching
MCP server caches:
- Autocomplete indices (rebuilt when role changes)
- Knowledge graph automata (loaded once per role)
- Document content (read from disk on demand)
Latency
Typical latency:
autocomplete_terms: 5-20ms (in-memory FST lookup)search: 50-200ms (knowledge graph traversal + file I/O)extract_paragraphs_from_automata: 10-50ms (linear scan + extraction)find_matches: 20-100ms (Aho-Corasick matching)
Memory Usage
Memory per role:
- Autocomplete index: ~5-10MB (depends on thesaurus size)
- Knowledge graph: ~20-50MB (nodes + edges + documents)
- Document cache: ~0MB (not cached by default)
Total: ~25-60MB per active role.
Comparison with Conare AI
| Feature | Conare AI | Terraphim MCP |
|---------|-----------|---------------|
| File References | "@" instant referencing | search + extract_paragraphs tools |
| Line Numbers | Automatic | Returned with paragraph extraction |
| Context Window | Full file | Configurable paragraphs or full file |
| Semantic Search | No | Yes (knowledge graph expansion) |
| Concept Matching | No | Yes (find_matches) |
| Autocomplete | Basic | Fuzzy + semantic expansion |
| Token Tracking | Built-in UI | Via document metadata |
| Cross-References | Manual | Automatic via knowledge graph |
Advantages of Terraphim:
- Semantic Understanding: Finds related concepts, not just keyword matches
- Knowledge Graph: Understands relationships between concepts
- Flexible Extraction: Get exactly the context you need (paragraph, function, etc.)
- Multi-Source: Search across local files, URLs, APIs simultaneously
- Extensible: Add custom tools via MCP protocol
Best Practices
1. Use Specific Search Terms
Good:
search({ query: "tokio::select cancellation", role: "Context Engineer" })Bad:
search({ query: "async", role: "Context Engineer" }) // Too broad2. Extract Minimal Context
Good:
extract_paragraphs_from_automata({
text: doc.body,
term: "tokio::spawn",
max_paragraphs: 1 // Only the immediate context
})Bad:
// Returning entire file when only one function needed
search({ query: "tokio", limit: 1 })3. Combine Tools for Rich Context
// 1. Find relevant documents
const docs = await search({ query: "async patterns" });
// 2. Extract specific examples
const examples = await extract_paragraphs({
text: docs[0].body,
term: "tokio::select"
});
// 3. Find related concepts
const related = await autocomplete_terms({
query: "tokio::select"
});
// Result: Full context with examples and related concepts4. Cache Autocomplete Index
// Build once per role
await mcp.callTool("build_autocomplete_index", {
role: "Context Engineer"
});
// Then use autocomplete freely
const suggestions = await mcp.callTool("autocomplete_terms", {
query: "async"
});Troubleshooting
MCP Tool Not Found
# Verify MCP server is running
|
# Check Claude Desktop logs
# Test MCP server directly
Empty Results
# Verify knowledge graph is built
|
# Check haystack configuration
|
# Rebuild if needed
Line Numbers Incorrect
Line numbers are 1-indexed (first line is line 1, not line 0).
If line numbers seem off:
- Check file encoding (must be UTF-8)
- Verify line endings (LF vs CRLF)
- Ensure no binary content in text files
Future Enhancements
Planned improvements to file context tools:
- Streaming Results: Stream large file contents to avoid memory issues
- Syntax-Aware Extraction: Extract complete functions/classes using AST
- Diff-Based Context: Show changes between versions with line references
- Multi-File Context: Extract related code across multiple files
- Token Budget Management: Automatic context truncation based on LLM limits
- IDE Integration: Direct jump-to-definition from MCP responses
See Also
- Conare Comparison - Full feature comparison
- MCP Integration - Claude Desktop setup
- Knowledge Graph System - How indexing works
- Vibe Rules - Coding rules and patterns