Concept: MCP Middleware Proxy
genshijin-shrink acts as a stdio middleware proxy positioned between a Claude client (or any MCP client) and an upstream MCP server. Its primary purpose is to reduce token consumption by intercepting and compressing the description fields within JSON-RPC responses.
How it intercepts tokens
When an MCP server provides tool metadata via the tools/list RPC, it often includes lengthy English descriptions. For example, a Filesystem MCP server can consume thousands of tokens per session just by listing tool descriptions. genshijin-shrink intercepts these responses and compresses only the prose descriptions while protecting technical identifiers.
Protection Mechanism
To ensure that code, URLs, and paths are not corrupted during compression, the proxy uses a sentinel replacement strategy. It identifies protected patterns, replaces them with unique sentinel strings, compresses the remaining prose using regex, and then restores the original tokens.
Protected patterns include:
- Fenced code blocks (
```...```) - Inline code (
`code`) - URLs (
https://...) - File paths
CONST_CASE identifiers- Dotted method calls (
object.method()) - Function calls (
function(args)) - Semantic versioning (
1.2.3)
Implementation Constraints
- stdio line-buffering: Uses line buffering for both directions to ensure JSON-RPC messages (one per line) are correctly reassembled even if
stdin/stdout are partially flushed. - Request pass-through: The proxy does not modify request bodies sent to the upstream server.
- Conservative mutation: It only intervenes in
tools/list, prompts/list, and resources/list responses, specifically targeting the description field. It does not mutate tools/call responses to avoid breaking downstream parsing.
// Protected patterns used by genshijin-shrink
const PROTECTED_PATTERNS = [
/```[\s\S]*?```/g, // fenced code
/`[^`\n]+`/g, // inline code
/\bhttps?:\/\/\S+/gi, // URLs
/\b[\w.-]*[\/\\][\w.\/\\\-]+/g, // paths
/\b[A-Z][A-Za-z0-9]*(?:_[A-Z][A-Za-z0-9]*)+\b/g, // CONST_CASE
/\b\w+\.\w+(?:\.\w+)*\(\)?/g, // dotted.method()
/[A-Za-z_][A-Za-z0-9_]*\s*\([^)]*\)/g, // function calls
/\b\d+\.\d+\.\d+\b/g, // semver
];