AgentSkillsCN

mcp-daemon-isolation

通过外部CLI为查询型MCP工具(LSP、搜索、数据库)实现上下文隔离。当MCP查询结果消耗过多上下文令牌时使用。

SKILL.md
--- frontmatter
name: mcp-daemon-isolation
description: Context isolation for query-type MCP tools (LSP, search, database) via external CLI. Use when MCP query results consume too many context tokens.
allowed-tools: ["Read", "Bash", "Grep", "Glob"]

MCP Daemon Isolation for Query-Type MCPs

External CLI pattern for isolating query-type MCP tool results from main context.

Query-Type MCP Definition

TypeExamplesCharacteristics
Query-typeSerena (LSP), Database, SearchUnpredictable result size, potentially thousands of tokens
Action-typeFile write, Git, DeploySmall results, success/failure focused

Identification criteria: Tools with find_*, search_*, get_*, list_* patterns


Problem

code
Claude Session
├── mcp__serena__find_symbol("UserService")
│   └── Result: 2,500 tokens (full JSON)    ← CONSUMED
└── Context budget: rapidly depleting

Daemon isolates tool definitions (~350 tokens × tools), but results still consume context.


Solution: External CLI + Structured Extraction

code
Claude Session
├── Bash: serena-query find_symbol UserService
│   └── stdout: "• UserService [Class] @ src/user.py:42"
│       (~50 tokens)                      ← MINIMAL
└── Full result stored: /tmp/serena-result.json

Key insight: Claude IS the LLM. Structured extraction provides 95%+ token savings.


Architecture

code
┌──────────────┐     ┌──────────────────┐     ┌──────────────┐
│ Claude       │────▶│ serena-query     │────▶│ Serena       │
│ Session      │     │ (external CLI)   │     │ Daemon :8765 │
└──────────────┘     └──────────────────┘     └──────────────┘
     Bash (~50 tok)     SSE + MCP Protocol       29 tools

For protocol details: Read("references/mcp-sse-protocol.md")


Structured Extractors

ToolFull JSONExtracted OutputSavings
list_dir~800📁 Dirs(12): hooks... 📄 Files(11)95%
get_symbols_overview~1,200Class(2): Config... Function(5)96%
find_symbol~2,000• UserService [Class] @ src/user.py:42-9897%
search_for_pattern~1,500Matches(15) in 8 files95%

For formatter code: Read("references/extractors-and-examples.md")


Usage

Output Modes

bash
serena-query find_symbol UserService                    # summary (default)
serena-query find_symbol UserService --mode location    # location only (for Read)
serena-query find_symbol UserService --mode full        # full JSON

Basic Commands

bash
serena-query list_dir .
serena-query get_symbols_overview src/main.py --depth 1
serena-query find_symbol UserService --path src/
serena-query search_for_pattern "class.*Service" --path src/
serena-query find_symbol UserService --output /tmp/result.json

Optimal Workflow: Explore-Locate-Read

code
1. Explore (--mode summary)   ~25 tokens  "What symbols exist?"
2. Locate (--mode location)   ~15 tokens  "Where exactly is it?"
3. Read (Read tool)           actual size  Only needed code
Scenariostdio approachdaemon+ReadSavings
Class analysis~525~24054%
11 function analysis~4,300~66585%
Large-scale search~10,000+~50095%

For detailed examples: Read("references/extractors-and-examples.md")


Installation

bash
pip install httpx
cp scripts/serena-query ~/.local/bin/
chmod +x ~/.local/bin/serena-query

Daemon Setup

bash
uvx --from git+https://github.com/oraios/serena \
  serena start-mcp-server --transport sse --port 8765 --project-from-cwd

Decision: Structured vs LLM Summarization

AspectLLM SummarizationStructured Extraction
Latency+2-5 seconds~0ms
CostAPI call per queryZero
ConsistencyVariableDeterministic

Claude IS the LLM consuming the output - no need for additional summarization.


Isolation Strategy Guide

ScenarioRecommendedReason
Single symbol editstdioImmediate modification
Codebase explorationdaemonExpect large results
Reference trackingdaemon + locationOnly need locations
Refactoring planningdaemon + fullFull structure analysis

Common Issues

SymptomCauseSolution
"Received request before initialization"Missing handshakeinitializenotifications/initializedtools/call sequence
"Connection refused on 8765"Daemon not runningsystemctl --user start serena-daemon
"Empty result"Structure mismatchSave raw JSON with --output and verify

References

Implementation

Protocol & Examples

Related