trueline-mcp 2.5.9 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # trueline-mcp
2
2
 
3
- [![CI](https://github.com/rjkaes/trueline-mcp/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/rjkaes/trueline-mcp/actions/workflows/ci.yml) [![License: Apache-2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE) [![Last commit](https://img.shields.io/github/last-commit/rjkaes/trueline-mcp)](https://github.com/rjkaes/trueline-mcp/commits/main) [![TypeScript](https://img.shields.io/badge/TypeScript-5.x-blue?logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
3
+ [![CI](https://github.com/rjkaes/trueline-mcp/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/rjkaes/trueline-mcp/actions/workflows/ci.yml)
4
4
 
5
- A [Model Context Protocol](https://modelcontextprotocol.io/) plugin that cuts
6
- context usage and catches editing mistakes. Works with Claude Code, Gemini CLI,
7
- VS Code Copilot, OpenCode, and Codex CLI.
5
+ A [Model Context Protocol](https://modelcontextprotocol.io/) plugin that reduces
6
+ context usage on large files and catches editing mistakes. Works with Claude
7
+ Code, Gemini CLI, VS Code Copilot, OpenCode, and Codex CLI.
8
8
 
9
9
  ## Installation
10
10
 
@@ -20,7 +20,7 @@ See [INSTALL.md](INSTALL.md) for platform-specific setup instructions.
20
20
 
21
21
  ## The problem
22
22
 
23
- AI agents waste tokens in two ways:
23
+ AI agents waste tokens on large files in two ways:
24
24
 
25
25
  1. **Reading too much.** To find a function in a 500-line file, the agent
26
26
  reads all 500 lines, most of which it doesn't need.
@@ -29,16 +29,19 @@ AI agents waste tokens in two ways:
29
29
  output the old text being replaced (`old_string`) plus the new text.
30
30
  The old text is pure overhead.
31
31
 
32
- Both problems compound. A typical editing session reads dozens of files
33
- and makes multiple edits, burning through context on redundant content.
32
+ Both problems compound on larger files. A typical editing session reads
33
+ dozens of files and makes multiple edits, burning through context on
34
+ redundant content.
34
35
 
35
36
  And when things go wrong (stale reads, hallucinated anchors, ambiguous
36
37
  matches) the agent silently corrupts your code.
37
38
 
38
39
  ## How trueline fixes this
39
40
 
40
- trueline replaces the built-in `Read` and `Edit` with six tools that
41
- are smaller, faster, and verified.
41
+ trueline provides six MCP tools that are smaller, faster, and verified.
42
+ For small files, the built-in tools work fine. For larger files, the
43
+ savings from targeted reads and compact edits outweigh the token
44
+ overhead of crossing the MCP protocol barrier.
42
45
 
43
46
  ### Read less: `trueline_outline` + `trueline_read`
44
47
 
@@ -60,6 +63,10 @@ classes, and declarations with their line ranges:
60
63
  12 lines instead of 139. The agent sees the full structure, then reads
61
64
  only the ranges it needs, skipping hundreds of irrelevant lines.
62
65
 
66
+ The savings scale with file size. MCP tool calls have per-call framing
67
+ overhead, so the break-even point is roughly 15KB; below that, a plain
68
+ `Read` is cheaper. Above it, the gap widens quickly:
69
+
63
70
  | File size | Full read | Outline | Savings |
64
71
  |-------------|-----------|---------|--------|
65
72
  | 140 lines | 140 tokens | 12 tokens | 91% |
@@ -114,33 +121,6 @@ No more silent corruption. No more ambiguous string matches.
114
121
  valid without re-reading the file. When the file hasn't changed (the
115
122
  common case), the response is a single line — near-zero tokens.
116
123
 
117
- ### Benchmarks
118
-
119
- Measured on real project files (`src/streaming-edit.ts`, 529 lines),
120
- comparing total bytes through the context window (call + result, ÷4 ≈
121
- tokens):
122
-
123
- | Workflow | Built-in | Trueline | Saved |
124
- |-------------------------|----------|-----------|-------|
125
- | Navigate & understand | 22 094 | 3 609 | 84% |
126
- | Explore then edit | 22 729 | 8 515 | 63% |
127
- | Search & fix | 22 731 | 812 | 96% |
128
- | Multi-region read | 22 094 | 2 720 | 88% |
129
- | Multi-file exploration | 39 296 | 1 761 | 96% |
130
- | Verify before edit | 44 823 | 3 608 | 92% |
131
- | **Total** |**173 767**| **21 025**| **88%** |
132
-
133
- The search-and-fix workflow saves the most: a single `trueline_search`
134
- call replaces grep + full-file read + old-string echo, cutting **96%**
135
- of token usage. The verify-before-edit workflow shows how
136
- `trueline_verify` avoids a full re-read when checking whether held
137
- checksums are still valid — **92%** savings over re-reading the entire
138
- file. Even the explore-then-edit workflow — which includes an
139
- exploratory read, a targeted re-read, and an edit — still saves **63%**
140
- over the built-in equivalent.
141
-
142
- Run the benchmark yourself: `bun run benchmarks/token-benchmark.ts`
143
-
144
124
  ## Design
145
125
 
146
126
  See [DESIGN.md](DESIGN.md) for the protocol specification, hash
package/dist/server.js CHANGED
@@ -42,7 +42,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
42
42
  `:`[${J[z]}\r
43
43
  ]`;continue}if(Y+=J[z],J[z]==="\\")G=!0;else if(W&&J[z]==="]")W=!1;else if(!W&&J[z]==="[")W=!0}try{new RegExp(Y)}catch{return console.warn(`Could not convert regex pattern at ${X.currentPath.join("/")} to a flag-independent form! Falling back to the flag-ignorant source`),Q.source}return Y}function SX(Q,X){if(X.target==="openAi")console.warn("Warning: OpenAI may not support records in schemas! Try an array of key-value pairs instead.");if(X.target==="openApi3"&&Q.keyType?._def.typeName===R.ZodEnum)return{type:"object",required:Q.keyType._def.values,properties:Q.keyType._def.values.reduce((J,Y)=>({...J,[Y]:l(Q.valueType._def,{...X,currentPath:[...X.currentPath,"properties",Y]})??M0(X)}),{}),additionalProperties:X.rejectedAdditionalProperties};let $={type:"object",additionalProperties:l(Q.valueType._def,{...X,currentPath:[...X.currentPath,"additionalProperties"]})??X.allowedAdditionalProperties};if(X.target==="openApi3")return $;if(Q.keyType?._def.typeName===R.ZodString&&Q.keyType._def.checks?.length){let{type:J,...Y}=RX(Q.keyType._def,X);return{...$,propertyNames:Y}}else if(Q.keyType?._def.typeName===R.ZodEnum)return{...$,propertyNames:{enum:Q.keyType._def.values}};else if(Q.keyType?._def.typeName===R.ZodBranded&&Q.keyType._def.type._def.typeName===R.ZodString&&Q.keyType._def.type._def.checks?.length){let{type:J,...Y}=MX(Q.keyType._def,X);return{...$,propertyNames:Y}}return $}function zW(Q,X){if(X.mapStrategy==="record")return SX(Q,X);let $=l(Q.keyType._def,{...X,currentPath:[...X.currentPath,"items","items","0"]})||M0(X),J=l(Q.valueType._def,{...X,currentPath:[...X.currentPath,"items","items","1"]})||M0(X);return{type:"array",maxItems:125,items:{type:"array",items:[$,J],minItems:2,maxItems:2}}}function KW(Q){let X=Q.values,J=Object.keys(Q.values).filter((G)=>{return typeof X[X[G]]!=="number"}).map((G)=>X[G]),Y=Array.from(new Set(J.map((G)=>typeof G)));return{type:Y.length===1?Y[0]==="string"?"string":"number":["string","number"],enum:J}}function qW(Q){return Q.target==="openAi"?void 0:{not:M0({...Q,currentPath:[...Q.currentPath,"not"]})}}function BW(Q){return Q.target==="openApi3"?{enum:["null"],nullable:!0}:{type:"null"}}var X8={ZodString:"string",ZodNumber:"number",ZodBigInt:"integer",ZodBoolean:"boolean",ZodNull:"null"};function UW(Q,X){if(X.target==="openApi3")return VW(Q,X);let $=Q.options instanceof Map?Array.from(Q.options.values()):Q.options;if($.every((J)=>(J._def.typeName in X8)&&(!J._def.checks||!J._def.checks.length))){let J=$.reduce((Y,G)=>{let W=X8[G._def.typeName];return W&&!Y.includes(W)?[...Y,W]:Y},[]);return{type:J.length>1?J:J[0]}}else if($.every((J)=>J._def.typeName==="ZodLiteral"&&!J.description)){let J=$.reduce((Y,G)=>{let W=typeof G._def.value;switch(W){case"string":case"number":case"boolean":return[...Y,W];case"bigint":return[...Y,"integer"];case"object":if(G._def.value===null)return[...Y,"null"];case"symbol":case"undefined":case"function":default:return Y}},[]);if(J.length===$.length){let Y=J.filter((G,W,H)=>H.indexOf(G)===W);return{type:Y.length>1?Y:Y[0],enum:$.reduce((G,W)=>{return G.includes(W._def.value)?G:[...G,W._def.value]},[])}}}else if($.every((J)=>J._def.typeName==="ZodEnum"))return{type:"string",enum:$.reduce((J,Y)=>[...J,...Y._def.values.filter((G)=>!J.includes(G))],[])};return VW(Q,X)}var VW=(Q,X)=>{let $=(Q.options instanceof Map?Array.from(Q.options.values()):Q.options).map((J,Y)=>l(J._def,{...X,currentPath:[...X.currentPath,"anyOf",`${Y}`]})).filter((J)=>!!J&&(!X.strictUnions||typeof J==="object"&&Object.keys(J).length>0));return $.length?{anyOf:$}:void 0};function AW(Q,X){if(["ZodString","ZodNumber","ZodBigInt","ZodBoolean","ZodNull"].includes(Q.innerType._def.typeName)&&(!Q.innerType._def.checks||!Q.innerType._def.checks.length)){if(X.target==="openApi3")return{type:X8[Q.innerType._def.typeName],nullable:!0};return{type:[X8[Q.innerType._def.typeName],"null"]}}if(X.target==="openApi3"){let J=l(Q.innerType._def,{...X,currentPath:[...X.currentPath]});if(J&&"$ref"in J)return{allOf:[J],nullable:!0};return J&&{...J,nullable:!0}}let $=l(Q.innerType._def,{...X,currentPath:[...X.currentPath,"anyOf","0"]});return $&&{anyOf:[$,{type:"null"}]}}function DW(Q,X){let $={type:"number"};if(!Q.checks)return $;for(let J of Q.checks)switch(J.kind){case"int":$.type="integer",sJ($,"type",J.message,X);break;case"min":if(X.target==="jsonSchema7")if(J.inclusive)X0($,"minimum",J.value,J.message,X);else X0($,"exclusiveMinimum",J.value,J.message,X);else{if(!J.inclusive)$.exclusiveMinimum=!0;X0($,"minimum",J.value,J.message,X)}break;case"max":if(X.target==="jsonSchema7")if(J.inclusive)X0($,"maximum",J.value,J.message,X);else X0($,"exclusiveMaximum",J.value,J.message,X);else{if(!J.inclusive)$.exclusiveMaximum=!0;X0($,"maximum",J.value,J.message,X)}break;case"multipleOf":X0($,"multipleOf",J.value,J.message,X);break}return $}function FW(Q,X){let $=X.target==="openAi",J={type:"object",properties:{}},Y=[],G=Q.shape();for(let H in G){let z=G[H];if(z===void 0||z._def===void 0)continue;let K=HD(z);if(K&&$){if(z._def.typeName==="ZodOptional")z=z._def.innerType;if(!z.isNullable())z=z.nullable();K=!1}let q=l(z._def,{...X,currentPath:[...X.currentPath,"properties",H],propertyPath:[...X.currentPath,"properties",H]});if(q===void 0)continue;if(J.properties[H]=q,!K)Y.push(H)}if(Y.length)J.required=Y;let W=WD(Q,X);if(W!==void 0)J.additionalProperties=W;return J}function WD(Q,X){if(Q.catchall._def.typeName!=="ZodNever")return l(Q.catchall._def,{...X,currentPath:[...X.currentPath,"additionalProperties"]});switch(Q.unknownKeys){case"passthrough":return X.allowedAdditionalProperties;case"strict":return X.rejectedAdditionalProperties;case"strip":return X.removeAdditionalStrategy==="strict"?X.allowedAdditionalProperties:X.rejectedAdditionalProperties}}function HD(Q){try{return Q.isOptional()}catch{return!0}}var OW=(Q,X)=>{if(X.currentPath.toString()===X.propertyPath?.toString())return l(Q.innerType._def,X);let $=l(Q.innerType._def,{...X,currentPath:[...X.currentPath,"anyOf","1"]});return $?{anyOf:[{not:M0(X)},$]}:M0(X)};var LW=(Q,X)=>{if(X.pipeStrategy==="input")return l(Q.in._def,X);else if(X.pipeStrategy==="output")return l(Q.out._def,X);let $=l(Q.in._def,{...X,currentPath:[...X.currentPath,"allOf","0"]}),J=l(Q.out._def,{...X,currentPath:[...X.currentPath,"allOf",$?"1":"0"]});return{allOf:[$,J].filter((Y)=>Y!==void 0)}};function wW(Q,X){return l(Q.type._def,X)}function jW(Q,X){let J={type:"array",uniqueItems:!0,items:l(Q.valueType._def,{...X,currentPath:[...X.currentPath,"items"]})};if(Q.minSize)X0(J,"minItems",Q.minSize.value,Q.minSize.message,X);if(Q.maxSize)X0(J,"maxItems",Q.maxSize.value,Q.maxSize.message,X);return J}function NW(Q,X){if(Q.rest)return{type:"array",minItems:Q.items.length,items:Q.items.map(($,J)=>l($._def,{...X,currentPath:[...X.currentPath,"items",`${J}`]})).reduce(($,J)=>J===void 0?$:[...$,J],[]),additionalItems:l(Q.rest._def,{...X,currentPath:[...X.currentPath,"additionalItems"]})};else return{type:"array",minItems:Q.items.length,maxItems:Q.items.length,items:Q.items.map(($,J)=>l($._def,{...X,currentPath:[...X.currentPath,"items",`${J}`]})).reduce(($,J)=>J===void 0?$:[...$,J],[])}}function EW(Q){return{not:M0(Q)}}function MW(Q){return M0(Q)}var RW=(Q,X)=>{return l(Q.innerType._def,X)};var SW=(Q,X,$)=>{switch(X){case R.ZodString:return RX(Q,$);case R.ZodNumber:return DW(Q,$);case R.ZodObject:return FW(Q,$);case R.ZodBigInt:return eG(Q,$);case R.ZodBoolean:return QW();case R.ZodDate:return eJ(Q,$);case R.ZodUndefined:return EW($);case R.ZodNull:return BW($);case R.ZodArray:return sG(Q,$);case R.ZodUnion:case R.ZodDiscriminatedUnion:return UW(Q,$);case R.ZodIntersection:return GW(Q,$);case R.ZodTuple:return NW(Q,$);case R.ZodRecord:return SX(Q,$);case R.ZodLiteral:return WW(Q,$);case R.ZodEnum:return YW(Q);case R.ZodNativeEnum:return KW(Q);case R.ZodNullable:return AW(Q,$);case R.ZodOptional:return OW(Q,$);case R.ZodMap:return zW(Q,$);case R.ZodSet:return jW(Q,$);case R.ZodLazy:return()=>Q.getter()._def;case R.ZodPromise:return wW(Q,$);case R.ZodNaN:case R.ZodNever:return qW($);case R.ZodEffects:return JW(Q,$);case R.ZodAny:return M0($);case R.ZodUnknown:return MW($);case R.ZodDefault:return $W(Q,$);case R.ZodBranded:return MX(Q,$);case R.ZodReadonly:return RW(Q,$);case R.ZodCatch:return XW(Q,$);case R.ZodPipeline:return LW(Q,$);case R.ZodFunction:case R.ZodVoid:case R.ZodSymbol:return;default:return((J)=>{return})(X)}};function l(Q,X,$=!1){let J=X.seen.get(Q);if(X.override){let H=X.override?.(Q,X,J,$);if(H!==rG)return H}if(J&&!$){let H=zD(J,X);if(H!==void 0)return H}let Y={def:Q,path:X.currentPath,jsonSchema:void 0};X.seen.set(Q,Y);let G=SW(Q,Q.typeName,X),W=typeof G==="function"?l(G(),X):G;if(W)KD(Q,X,W);if(X.postProcess){let H=X.postProcess(W,Q,X);return Y.jsonSchema=W,H}return Y.jsonSchema=W,W}var zD=(Q,X)=>{switch(X.$refStrategy){case"root":return{$ref:Q.path.join("/")};case"relative":return{$ref:EX(X.currentPath,Q.path)};case"none":case"seen":{if(Q.path.length<X.currentPath.length&&Q.path.every(($,J)=>X.currentPath[J]===$))return console.warn(`Recursive reference detected at ${X.currentPath.join("/")}! Defaulting to any`),M0(X);return X.$refStrategy==="seen"?M0(X):void 0}}},KD=(Q,X,$)=>{if(Q.description){if($.description=Q.description,X.markdownDescription)$.markdownDescription=Q.description}return $};var $7=(Q,X)=>{let $=aG(X),J=typeof X==="object"&&X.definitions?Object.entries(X.definitions).reduce((z,[K,q])=>({...z,[K]:l(q._def,{...$,currentPath:[...$.basePath,$.definitionPath,K]},!0)??M0($)}),{}):void 0,Y=typeof X==="string"?X:X?.nameStrategy==="title"?void 0:X?.name,G=l(Q._def,Y===void 0?$:{...$,currentPath:[...$.basePath,$.definitionPath,Y]},!1)??M0($),W=typeof X==="object"&&X.name!==void 0&&X.nameStrategy==="title"?X.name:void 0;if(W!==void 0)G.title=W;if($.flags.hasReferencedOpenAiAnyType){if(!J)J={};if(!J[$.openAiAnyTypeName])J[$.openAiAnyTypeName]={type:["string","number","integer","boolean","array","null"],items:{$ref:$.$refStrategy==="relative"?"1":[...$.basePath,$.definitionPath,$.openAiAnyTypeName].join("/")}}}let H=Y===void 0?J?{...G,[$.definitionPath]:J}:G:{$ref:[...$.$refStrategy==="relative"?[]:$.basePath,$.definitionPath,Y].join("/"),[$.definitionPath]:{...J,[Y]:G}};if($.target==="jsonSchema7")H.$schema="http://json-schema.org/draft-07/schema#";else if($.target==="jsonSchema2019-09"||$.target==="openAi")H.$schema="https://json-schema.org/draft/2019-09/schema#";if($.target==="openAi"&&(("anyOf"in H)||("oneOf"in H)||("allOf"in H)||("type"in H)&&Array.isArray(H.type)))console.warn("Warning: OpenAI may not support schemas with unions as roots! Try wrapping it in an object property.");return H};function qD(Q){if(!Q)return"draft-7";if(Q==="jsonSchema7"||Q==="draft-7")return"draft-7";if(Q==="jsonSchema2019-09"||Q==="draft-2020-12")return"draft-2020-12";return"draft-7"}function J7(Q,X){if(z6(Q))return SJ(Q,{target:qD(X?.target),io:X?.pipeStrategy??"input"});return $7(Q,{strictUnions:X?.strictUnions??!0,pipeStrategy:X?.pipeStrategy??"input"})}function Y7(Q){let $=J9(Q)?.method;if(!$)throw Error("Schema is missing a method literal");let J=s8($);if(typeof J!=="string")throw Error("Schema method literal must be a string");return J}function G7(Q,X){let $=$9(Q,X);if(!$.success)throw $.error;return $.data}var BD=60000;class W7{constructor(Q){if(this._options=Q,this._requestMessageId=0,this._requestHandlers=new Map,this._requestHandlerAbortControllers=new Map,this._notificationHandlers=new Map,this._responseHandlers=new Map,this._progressHandlers=new Map,this._timeoutInfo=new Map,this._pendingDebouncedNotifications=new Set,this._taskProgressTokens=new Map,this._requestResolvers=new Map,this.setNotificationHandler(GX,(X)=>{this._oncancel(X)}),this.setNotificationHandler(HX,(X)=>{this._onprogress(X)}),this.setRequestHandler(WX,(X)=>({})),this._taskStore=Q?.taskStore,this._taskMessageQueue=Q?.taskMessageQueue,this._taskStore)this.setRequestHandler(zX,async(X,$)=>{let J=await this._taskStore.getTask(X.params.taskId,$.sessionId);if(!J)throw new g(f.InvalidParams,"Failed to retrieve task: Task not found");return{...J}}),this.setRequestHandler(qX,async(X,$)=>{let J=async()=>{let Y=X.params.taskId;if(this._taskMessageQueue){let W;while(W=await this._taskMessageQueue.dequeue(Y,$.sessionId)){if(W.type==="response"||W.type==="error"){let H=W.message,z=H.id,K=this._requestResolvers.get(z);if(K)if(this._requestResolvers.delete(z),W.type==="response")K(H);else{let q=H,B=new g(q.error.code,q.error.message,q.error.data);K(B)}else{let q=W.type==="response"?"Response":"Error";this._onerror(Error(`${q} handler missing for request ${z}`))}continue}await this._transport?.send(W.message,{relatedRequestId:$.requestId})}}let G=await this._taskStore.getTask(Y,$.sessionId);if(!G)throw new g(f.InvalidParams,`Task not found: ${Y}`);if(!G9(G.status))return await this._waitForTaskUpdate(Y,$.signal),await J();if(G9(G.status)){let W=await this._taskStore.getTaskResult(Y,$.sessionId);return this._clearTaskQueue(Y),{...W,_meta:{...W._meta,[Y9]:{taskId:Y}}}}return await J()};return await J()}),this.setRequestHandler(BX,async(X,$)=>{try{let{tasks:J,nextCursor:Y}=await this._taskStore.listTasks(X.params?.cursor,$.sessionId);return{tasks:J,nextCursor:Y,_meta:{}}}catch(J){throw new g(f.InvalidParams,`Failed to list tasks: ${J instanceof Error?J.message:String(J)}`)}}),this.setRequestHandler(UX,async(X,$)=>{try{let J=await this._taskStore.getTask(X.params.taskId,$.sessionId);if(!J)throw new g(f.InvalidParams,`Task not found: ${X.params.taskId}`);if(G9(J.status))throw new g(f.InvalidParams,`Cannot cancel task in terminal status: ${J.status}`);await this._taskStore.updateTaskStatus(X.params.taskId,"cancelled","Client cancelled task execution.",$.sessionId),this._clearTaskQueue(X.params.taskId);let Y=await this._taskStore.getTask(X.params.taskId,$.sessionId);if(!Y)throw new g(f.InvalidParams,`Task not found after cancellation: ${X.params.taskId}`);return{_meta:{},...Y}}catch(J){if(J instanceof g)throw J;throw new g(f.InvalidRequest,`Failed to cancel task: ${J instanceof Error?J.message:String(J)}`)}})}async _oncancel(Q){if(!Q.params.requestId)return;this._requestHandlerAbortControllers.get(Q.params.requestId)?.abort(Q.params.reason)}_setupTimeout(Q,X,$,J,Y=!1){this._timeoutInfo.set(Q,{timeoutId:setTimeout(J,X),startTime:Date.now(),timeout:X,maxTotalTimeout:$,resetTimeoutOnProgress:Y,onTimeout:J})}_resetTimeout(Q){let X=this._timeoutInfo.get(Q);if(!X)return!1;let $=Date.now()-X.startTime;if(X.maxTotalTimeout&&$>=X.maxTotalTimeout)throw this._timeoutInfo.delete(Q),g.fromError(f.RequestTimeout,"Maximum total timeout exceeded",{maxTotalTimeout:X.maxTotalTimeout,totalElapsed:$});return clearTimeout(X.timeoutId),X.timeoutId=setTimeout(X.onTimeout,X.timeout),!0}_cleanupTimeout(Q){let X=this._timeoutInfo.get(Q);if(X)clearTimeout(X.timeoutId),this._timeoutInfo.delete(Q)}async connect(Q){if(this._transport)throw Error("Already connected to a transport. Call close() before connecting to a new transport, or use a separate Protocol instance per connection.");this._transport=Q;let X=this.transport?.onclose;this._transport.onclose=()=>{X?.(),this._onclose()};let $=this.transport?.onerror;this._transport.onerror=(Y)=>{$?.(Y),this._onerror(Y)};let J=this._transport?.onmessage;this._transport.onmessage=(Y,G)=>{if(J?.(Y,G),nQ(Y)||gG(Y))this._onresponse(Y);else if(hJ(Y))this._onrequest(Y,G);else if(xG(Y))this._onnotification(Y);else this._onerror(Error(`Unknown message type: ${JSON.stringify(Y)}`))},await this._transport.start()}_onclose(){let Q=this._responseHandlers;this._responseHandlers=new Map,this._progressHandlers.clear(),this._taskProgressTokens.clear(),this._pendingDebouncedNotifications.clear();for(let $ of this._requestHandlerAbortControllers.values())$.abort();this._requestHandlerAbortControllers.clear();let X=g.fromError(f.ConnectionClosed,"Connection closed");this._transport=void 0,this.onclose?.();for(let $ of Q.values())$(X)}_onerror(Q){this.onerror?.(Q)}_onnotification(Q){let X=this._notificationHandlers.get(Q.method)??this.fallbackNotificationHandler;if(X===void 0)return;Promise.resolve().then(()=>X(Q)).catch(($)=>this._onerror(Error(`Uncaught error in notification handler: ${$}`)))}_onrequest(Q,X){let $=this._requestHandlers.get(Q.method)??this.fallbackRequestHandler,J=this._transport,Y=Q.params?._meta?.[Y9]?.taskId;if($===void 0){let K={jsonrpc:"2.0",id:Q.id,error:{code:f.MethodNotFound,message:"Method not found"}};if(Y&&this._taskMessageQueue)this._enqueueTaskMessage(Y,{type:"error",message:K,timestamp:Date.now()},J?.sessionId).catch((q)=>this._onerror(Error(`Failed to enqueue error response: ${q}`)));else J?.send(K).catch((q)=>this._onerror(Error(`Failed to send an error response: ${q}`)));return}let G=new AbortController;this._requestHandlerAbortControllers.set(Q.id,G);let W=kG(Q.params)?Q.params.task:void 0,H=this._taskStore?this.requestTaskStore(Q,J?.sessionId):void 0,z={signal:G.signal,sessionId:J?.sessionId,_meta:Q.params?._meta,sendNotification:async(K)=>{if(G.signal.aborted)return;let q={relatedRequestId:Q.id};if(Y)q.relatedTask={taskId:Y};await this.notification(K,q)},sendRequest:async(K,q,B)=>{if(G.signal.aborted)throw new g(f.ConnectionClosed,"Request was cancelled");let A={...B,relatedRequestId:Q.id};if(Y&&!A.relatedTask)A.relatedTask={taskId:Y};let U=A.relatedTask?.taskId??Y;if(U&&H)await H.updateTaskStatus(U,"input_required");return await this.request(K,q,A)},authInfo:X?.authInfo,requestId:Q.id,requestInfo:X?.requestInfo,taskId:Y,taskStore:H,taskRequestedTtl:W?.ttl,closeSSEStream:X?.closeSSEStream,closeStandaloneSSEStream:X?.closeStandaloneSSEStream};Promise.resolve().then(()=>{if(W)this.assertTaskHandlerCapability(Q.method)}).then(()=>$(Q,z)).then(async(K)=>{if(G.signal.aborted)return;let q={result:K,jsonrpc:"2.0",id:Q.id};if(Y&&this._taskMessageQueue)await this._enqueueTaskMessage(Y,{type:"response",message:q,timestamp:Date.now()},J?.sessionId);else await J?.send(q)},async(K)=>{if(G.signal.aborted)return;let q={jsonrpc:"2.0",id:Q.id,error:{code:Number.isSafeInteger(K.code)?K.code:f.InternalError,message:K.message??"Internal error",...K.data!==void 0&&{data:K.data}}};if(Y&&this._taskMessageQueue)await this._enqueueTaskMessage(Y,{type:"error",message:q,timestamp:Date.now()},J?.sessionId);else await J?.send(q)}).catch((K)=>this._onerror(Error(`Failed to send response: ${K}`))).finally(()=>{this._requestHandlerAbortControllers.delete(Q.id)})}_onprogress(Q){let{progressToken:X,...$}=Q.params,J=Number(X),Y=this._progressHandlers.get(J);if(!Y){this._onerror(Error(`Received a progress notification for an unknown token: ${JSON.stringify(Q)}`));return}let G=this._responseHandlers.get(J),W=this._timeoutInfo.get(J);if(W&&G&&W.resetTimeoutOnProgress)try{this._resetTimeout(J)}catch(H){this._responseHandlers.delete(J),this._progressHandlers.delete(J),this._cleanupTimeout(J),G(H);return}Y($)}_onresponse(Q){let X=Number(Q.id),$=this._requestResolvers.get(X);if($){if(this._requestResolvers.delete(X),nQ(Q))$(Q);else{let G=new g(Q.error.code,Q.error.message,Q.error.data);$(G)}return}let J=this._responseHandlers.get(X);if(J===void 0){this._onerror(Error(`Received a response for an unknown message ID: ${JSON.stringify(Q)}`));return}this._responseHandlers.delete(X),this._cleanupTimeout(X);let Y=!1;if(nQ(Q)&&Q.result&&typeof Q.result==="object"){let G=Q.result;if(G.task&&typeof G.task==="object"){let W=G.task;if(typeof W.taskId==="string")Y=!0,this._taskProgressTokens.set(W.taskId,X)}}if(!Y)this._progressHandlers.delete(X);if(nQ(Q))J(Q);else{let G=g.fromError(Q.error.code,Q.error.message,Q.error.data);J(G)}}get transport(){return this._transport}async close(){await this._transport?.close()}async*requestStream(Q,X,$){let{task:J}=$??{};if(!J){try{yield{type:"result",result:await this.request(Q,X,$)}}catch(G){yield{type:"error",error:G instanceof g?G:new g(f.InternalError,String(G))}}return}let Y;try{let G=await this.request(Q,$Q,$);if(G.task)Y=G.task.taskId,yield{type:"taskCreated",task:G.task};else throw new g(f.InternalError,"Task creation did not return a task");while(!0){let W=await this.getTask({taskId:Y},$);if(yield{type:"taskStatus",task:W},G9(W.status)){if(W.status==="completed")yield{type:"result",result:await this.getTaskResult({taskId:Y},X,$)};else if(W.status==="failed")yield{type:"error",error:new g(f.InternalError,`Task ${Y} failed`)};else if(W.status==="cancelled")yield{type:"error",error:new g(f.InternalError,`Task ${Y} was cancelled`)};return}if(W.status==="input_required"){yield{type:"result",result:await this.getTaskResult({taskId:Y},X,$)};return}let H=W.pollInterval??this._options?.defaultTaskPollInterval??1000;await new Promise((z)=>setTimeout(z,H)),$?.signal?.throwIfAborted()}}catch(G){yield{type:"error",error:G instanceof g?G:new g(f.InternalError,String(G))}}}request(Q,X,$){let{relatedRequestId:J,resumptionToken:Y,onresumptiontoken:G,task:W,relatedTask:H}=$??{};return new Promise((z,K)=>{let q=(O)=>{K(O)};if(!this._transport){q(Error("Not connected"));return}if(this._options?.enforceStrictCapabilities===!0)try{if(this.assertCapabilityForMethod(Q.method),W)this.assertTaskCapability(Q.method)}catch(O){q(O);return}$?.signal?.throwIfAborted();let B=this._requestMessageId++,A={...Q,jsonrpc:"2.0",id:B};if($?.onprogress)this._progressHandlers.set(B,$.onprogress),A.params={...Q.params,_meta:{...Q.params?._meta||{},progressToken:B}};if(W)A.params={...A.params,task:W};if(H)A.params={...A.params,_meta:{...A.params?._meta||{},[Y9]:H}};let U=(O)=>{this._responseHandlers.delete(B),this._progressHandlers.delete(B),this._cleanupTimeout(B),this._transport?.send({jsonrpc:"2.0",method:"notifications/cancelled",params:{requestId:B,reason:String(O)}},{relatedRequestId:J,resumptionToken:Y,onresumptiontoken:G}).catch((E)=>this._onerror(Error(`Failed to send cancellation: ${E}`)));let w=O instanceof g?O:new g(f.RequestTimeout,String(O));K(w)};this._responseHandlers.set(B,(O)=>{if($?.signal?.aborted)return;if(O instanceof Error)return K(O);try{let w=$9(X,O.result);if(!w.success)K(w.error);else z(w.data)}catch(w){K(w)}}),$?.signal?.addEventListener("abort",()=>{U($?.signal?.reason)});let D=$?.timeout??BD,F=()=>U(g.fromError(f.RequestTimeout,"Request timed out",{timeout:D}));this._setupTimeout(B,D,$?.maxTotalTimeout,F,$?.resetTimeoutOnProgress??!1);let L=H?.taskId;if(L){let O=(w)=>{let E=this._responseHandlers.get(B);if(E)E(w);else this._onerror(Error(`Response handler missing for side-channeled request ${B}`))};this._requestResolvers.set(B,O),this._enqueueTaskMessage(L,{type:"request",message:A,timestamp:Date.now()}).catch((w)=>{this._cleanupTimeout(B),K(w)})}else this._transport.send(A,{relatedRequestId:J,resumptionToken:Y,onresumptiontoken:G}).catch((O)=>{this._cleanupTimeout(B),K(O)})})}async getTask(Q,X){return this.request({method:"tasks/get",params:Q},KX,X)}async getTaskResult(Q,X,$){return this.request({method:"tasks/result",params:Q},X,$)}async listTasks(Q,X){return this.request({method:"tasks/list",params:Q},VX,X)}async cancelTask(Q,X){return this.request({method:"tasks/cancel",params:Q},fG,X)}async notification(Q,X){if(!this._transport)throw Error("Not connected");this.assertNotificationCapability(Q.method);let $=X?.relatedTask?.taskId;if($){let W={...Q,jsonrpc:"2.0",params:{...Q.params,_meta:{...Q.params?._meta||{},[Y9]:X.relatedTask}}};await this._enqueueTaskMessage($,{type:"notification",message:W,timestamp:Date.now()});return}if((this._options?.debouncedNotificationMethods??[]).includes(Q.method)&&!Q.params&&!X?.relatedRequestId&&!X?.relatedTask){if(this._pendingDebouncedNotifications.has(Q.method))return;this._pendingDebouncedNotifications.add(Q.method),Promise.resolve().then(()=>{if(this._pendingDebouncedNotifications.delete(Q.method),!this._transport)return;let W={...Q,jsonrpc:"2.0"};if(X?.relatedTask)W={...W,params:{...W.params,_meta:{...W.params?._meta||{},[Y9]:X.relatedTask}}};this._transport?.send(W,X).catch((H)=>this._onerror(H))});return}let G={...Q,jsonrpc:"2.0"};if(X?.relatedTask)G={...G,params:{...G.params,_meta:{...G.params?._meta||{},[Y9]:X.relatedTask}}};await this._transport.send(G,X)}setRequestHandler(Q,X){let $=Y7(Q);this.assertRequestHandlerCapability($),this._requestHandlers.set($,(J,Y)=>{let G=G7(Q,J);return Promise.resolve(X(G,Y))})}removeRequestHandler(Q){this._requestHandlers.delete(Q)}assertCanSetRequestHandler(Q){if(this._requestHandlers.has(Q))throw Error(`A request handler for ${Q} already exists, which would be overridden`)}setNotificationHandler(Q,X){let $=Y7(Q);this._notificationHandlers.set($,(J)=>{let Y=G7(Q,J);return Promise.resolve(X(Y))})}removeNotificationHandler(Q){this._notificationHandlers.delete(Q)}_cleanupTaskProgressHandler(Q){let X=this._taskProgressTokens.get(Q);if(X!==void 0)this._progressHandlers.delete(X),this._taskProgressTokens.delete(Q)}async _enqueueTaskMessage(Q,X,$){if(!this._taskStore||!this._taskMessageQueue)throw Error("Cannot enqueue task message: taskStore and taskMessageQueue are not configured");let J=this._options?.maxTaskQueueSize;await this._taskMessageQueue.enqueue(Q,X,$,J)}async _clearTaskQueue(Q,X){if(this._taskMessageQueue){let $=await this._taskMessageQueue.dequeueAll(Q,X);for(let J of $)if(J.type==="request"&&hJ(J.message)){let Y=J.message.id,G=this._requestResolvers.get(Y);if(G)G(new g(f.InternalError,"Task cancelled or completed")),this._requestResolvers.delete(Y);else this._onerror(Error(`Resolver missing for request ${Y} during task ${Q} cleanup`))}}}async _waitForTaskUpdate(Q,X){let $=this._options?.defaultTaskPollInterval??1000;try{let J=await this._taskStore?.getTask(Q);if(J?.pollInterval)$=J.pollInterval}catch{}return new Promise((J,Y)=>{if(X.aborted){Y(new g(f.InvalidRequest,"Request cancelled"));return}let G=setTimeout(J,$);X.addEventListener("abort",()=>{clearTimeout(G),Y(new g(f.InvalidRequest,"Request cancelled"))},{once:!0})})}requestTaskStore(Q,X){let $=this._taskStore;if(!$)throw Error("No task store configured");return{createTask:async(J)=>{if(!Q)throw Error("No request provided");return await $.createTask(J,Q.id,{method:Q.method,params:Q.params},X)},getTask:async(J)=>{let Y=await $.getTask(J,X);if(!Y)throw new g(f.InvalidParams,"Failed to retrieve task: Task not found");return Y},storeTaskResult:async(J,Y,G)=>{await $.storeTaskResult(J,Y,G,X);let W=await $.getTask(J,X);if(W){let H=aQ.parse({method:"notifications/tasks/status",params:W});if(await this.notification(H),G9(W.status))this._cleanupTaskProgressHandler(J)}},getTaskResult:(J)=>{return $.getTaskResult(J,X)},updateTaskStatus:async(J,Y,G)=>{let W=await $.getTask(J,X);if(!W)throw new g(f.InvalidParams,`Task "${J}" not found - it may have been cleaned up`);if(G9(W.status))throw new g(f.InvalidParams,`Cannot update task "${J}" from terminal status "${W.status}" to "${Y}". Terminal states (completed, failed, cancelled) cannot transition to other states.`);await $.updateTaskStatus(J,Y,G,X);let H=await $.getTask(J,X);if(H){let z=aQ.parse({method:"notifications/tasks/status",params:H});if(await this.notification(z),G9(H.status))this._cleanupTaskProgressHandler(J)}},listTasks:(J)=>{return $.listTasks(J,X)}}}}function PW(Q){return Q!==null&&typeof Q==="object"&&!Array.isArray(Q)}function bW(Q,X){let $={...Q};for(let J in X){let Y=J,G=X[Y];if(G===void 0)continue;let W=$[Y];if(PW(W)&&PW(G))$[Y]={...W,...G};else $[Y]=G}return $}var Y1=N4(QY(),1),G1=N4(J1(),1);function NM(){let Q=new Y1.default({strict:!1,validateFormats:!0,validateSchema:!1,allErrors:!0});return G1.default(Q),Q}class qY{constructor(Q){this._ajv=Q??NM()}getValidator(Q){let X="$id"in Q&&typeof Q.$id==="string"?this._ajv.getSchema(Q.$id)??this._ajv.compile(Q):this._ajv.compile(Q);return($)=>{if(X($))return{valid:!0,data:$,errorMessage:void 0};else return{valid:!1,data:void 0,errorMessage:this._ajv.errorsText(X.errors)}}}}class BY{constructor(Q){this._server=Q}requestStream(Q,X,$){return this._server.requestStream(Q,X,$)}createMessageStream(Q,X){let $=this._server.getClientCapabilities();if((Q.tools||Q.toolChoice)&&!$?.sampling?.tools)throw Error("Client does not support sampling tools capability.");if(Q.messages.length>0){let J=Q.messages[Q.messages.length-1],Y=Array.isArray(J.content)?J.content:[J.content],G=Y.some((K)=>K.type==="tool_result"),W=Q.messages.length>1?Q.messages[Q.messages.length-2]:void 0,H=W?Array.isArray(W.content)?W.content:[W.content]:[],z=H.some((K)=>K.type==="tool_use");if(G){if(Y.some((K)=>K.type!=="tool_result"))throw Error("The last message must contain only tool_result content if any is present");if(!z)throw Error("tool_result blocks are not matching any tool_use from the previous message")}if(z){let K=new Set(H.filter((B)=>B.type==="tool_use").map((B)=>B.id)),q=new Set(Y.filter((B)=>B.type==="tool_result").map((B)=>B.toolUseId));if(K.size!==q.size||![...K].every((B)=>q.has(B)))throw Error("ids of tool_result blocks and tool_use blocks from previous message do not match")}}return this.requestStream({method:"sampling/createMessage",params:Q},Q8,X)}elicitInputStream(Q,X){let $=this._server.getClientCapabilities(),J=Q.mode??"form";switch(J){case"url":{if(!$?.elicitation?.url)throw Error("Client does not support url elicitation.");break}case"form":{if(!$?.elicitation?.form)throw Error("Client does not support form elicitation.");break}}let Y=J==="form"&&Q.mode===void 0?{...Q,mode:"form"}:Q;return this.requestStream({method:"elicitation/create",params:Y},GQ,X)}async getTask(Q,X){return this._server.getTask({taskId:Q},X)}async getTaskResult(Q,X,$){return this._server.getTaskResult({taskId:Q},X,$)}async listTasks(Q,X){return this._server.listTasks(Q?{cursor:Q}:void 0,X)}async cancelTask(Q,X){return this._server.cancelTask({taskId:Q},X)}}function W1(Q,X,$){if(!Q)throw Error(`${$} does not support task creation (required for ${X})`);switch(X){case"tools/call":if(!Q.tools?.call)throw Error(`${$} does not support task creation for tools/call (required for ${X})`);break;default:break}}function H1(Q,X,$){if(!Q)throw Error(`${$} does not support task creation (required for ${X})`);switch(X){case"sampling/createMessage":if(!Q.sampling?.createMessage)throw Error(`${$} does not support task creation for sampling/createMessage (required for ${X})`);break;case"elicitation/create":if(!Q.elicitation?.create)throw Error(`${$} does not support task creation for elicitation/create (required for ${X})`);break;default:break}}class VY extends W7{constructor(Q,X){super(X);if(this._serverInfo=Q,this._loggingLevels=new Map,this.LOG_LEVEL_SEVERITY=new Map(eQ.options.map(($,J)=>[$,J])),this.isMessageIgnored=($,J)=>{let Y=this._loggingLevels.get(J);return Y?this.LOG_LEVEL_SEVERITY.get($)<this.LOG_LEVEL_SEVERITY.get(Y):!1},this._capabilities=X?.capabilities??{},this._instructions=X?.instructions,this._jsonSchemaValidator=X?.jsonSchemaValidator??new qY,this.setRequestHandler(mJ,($)=>this._oninitialize($)),this.setNotificationHandler(lJ,()=>this.oninitialized?.()),this._capabilities.logging)this.setRequestHandler(rJ,async($,J)=>{let Y=J.sessionId||J.requestInfo?.headers["mcp-session-id"]||void 0,{level:G}=$.params,W=eQ.safeParse(G);if(W.success)this._loggingLevels.set(Y,W.data);return{}})}get experimental(){if(!this._experimental)this._experimental={tasks:new BY(this)};return this._experimental}registerCapabilities(Q){if(this.transport)throw Error("Cannot register capabilities after connecting to transport");this._capabilities=bW(this._capabilities,Q)}setRequestHandler(Q,X){let J=J9(Q)?.method;if(!J)throw Error("Schema is missing a method literal");let Y;if(z6(J)){let W=J;Y=W._zod?.def?.value??W.value}else{let W=J;Y=W._def?.value??W.value}if(typeof Y!=="string")throw Error("Schema method literal must be a string");if(Y==="tools/call"){let W=async(H,z)=>{let K=$9(YQ,H);if(!K.success){let U=K.error instanceof Error?K.error.message:String(K.error);throw new g(f.InvalidParams,`Invalid tools/call request: ${U}`)}let{params:q}=K.data,B=await Promise.resolve(X(H,z));if(q.task){let U=$9($Q,B);if(!U.success){let D=U.error instanceof Error?U.error.message:String(U.error);throw new g(f.InvalidParams,`Invalid task creation result: ${D}`)}return U.data}let A=$9(jX,B);if(!A.success){let U=A.error instanceof Error?A.error.message:String(A.error);throw new g(f.InvalidParams,`Invalid tools/call result: ${U}`)}return A.data};return super.setRequestHandler(Q,W)}return super.setRequestHandler(Q,X)}assertCapabilityForMethod(Q){switch(Q){case"sampling/createMessage":if(!this._clientCapabilities?.sampling)throw Error(`Client does not support sampling (required for ${Q})`);break;case"elicitation/create":if(!this._clientCapabilities?.elicitation)throw Error(`Client does not support elicitation (required for ${Q})`);break;case"roots/list":if(!this._clientCapabilities?.roots)throw Error(`Client does not support listing roots (required for ${Q})`);break;case"ping":break}}assertNotificationCapability(Q){switch(Q){case"notifications/message":if(!this._capabilities.logging)throw Error(`Server does not support logging (required for ${Q})`);break;case"notifications/resources/updated":case"notifications/resources/list_changed":if(!this._capabilities.resources)throw Error(`Server does not support notifying about resources (required for ${Q})`);break;case"notifications/tools/list_changed":if(!this._capabilities.tools)throw Error(`Server does not support notifying of tool list changes (required for ${Q})`);break;case"notifications/prompts/list_changed":if(!this._capabilities.prompts)throw Error(`Server does not support notifying of prompt list changes (required for ${Q})`);break;case"notifications/elicitation/complete":if(!this._clientCapabilities?.elicitation?.url)throw Error(`Client does not support URL elicitation (required for ${Q})`);break;case"notifications/cancelled":break;case"notifications/progress":break}}assertRequestHandlerCapability(Q){if(!this._capabilities)return;switch(Q){case"completion/complete":if(!this._capabilities.completions)throw Error(`Server does not support completions (required for ${Q})`);break;case"logging/setLevel":if(!this._capabilities.logging)throw Error(`Server does not support logging (required for ${Q})`);break;case"prompts/get":case"prompts/list":if(!this._capabilities.prompts)throw Error(`Server does not support prompts (required for ${Q})`);break;case"resources/list":case"resources/templates/list":case"resources/read":if(!this._capabilities.resources)throw Error(`Server does not support resources (required for ${Q})`);break;case"tools/call":case"tools/list":if(!this._capabilities.tools)throw Error(`Server does not support tools (required for ${Q})`);break;case"tasks/get":case"tasks/list":case"tasks/result":case"tasks/cancel":if(!this._capabilities.tasks)throw Error(`Server does not support tasks capability (required for ${Q})`);break;case"ping":case"initialize":break}}assertTaskCapability(Q){H1(this._clientCapabilities?.tasks?.requests,Q,"Client")}assertTaskHandlerCapability(Q){if(!this._capabilities)return;W1(this._capabilities.tasks?.requests,Q,"Server")}async _oninitialize(Q){let X=Q.params.protocolVersion;return this._clientCapabilities=Q.params.capabilities,this._clientVersion=Q.params.clientInfo,{protocolVersion:ZG.includes(X)?X:gJ,capabilities:this.getCapabilities(),serverInfo:this._serverInfo,...this._instructions&&{instructions:this._instructions}}}getClientCapabilities(){return this._clientCapabilities}getClientVersion(){return this._clientVersion}getCapabilities(){return this._capabilities}async ping(){return this.request({method:"ping"},YX)}async createMessage(Q,X){if(Q.tools||Q.toolChoice){if(!this._clientCapabilities?.sampling?.tools)throw Error("Client does not support sampling tools capability.")}if(Q.messages.length>0){let $=Q.messages[Q.messages.length-1],J=Array.isArray($.content)?$.content:[$.content],Y=J.some((z)=>z.type==="tool_result"),G=Q.messages.length>1?Q.messages[Q.messages.length-2]:void 0,W=G?Array.isArray(G.content)?G.content:[G.content]:[],H=W.some((z)=>z.type==="tool_use");if(Y){if(J.some((z)=>z.type!=="tool_result"))throw Error("The last message must contain only tool_result content if any is present");if(!H)throw Error("tool_result blocks are not matching any tool_use from the previous message")}if(H){let z=new Set(W.filter((q)=>q.type==="tool_use").map((q)=>q.id)),K=new Set(J.filter((q)=>q.type==="tool_result").map((q)=>q.toolUseId));if(z.size!==K.size||![...z].every((q)=>K.has(q)))throw Error("ids of tool_result blocks and tool_use blocks from previous message do not match")}}if(Q.tools)return this.request({method:"sampling/createMessage",params:Q},tJ,X);return this.request({method:"sampling/createMessage",params:Q},Q8,X)}async elicitInput(Q,X){switch(Q.mode??"form"){case"url":{if(!this._clientCapabilities?.elicitation?.url)throw Error("Client does not support url elicitation.");let J=Q;return this.request({method:"elicitation/create",params:J},GQ,X)}case"form":{if(!this._clientCapabilities?.elicitation?.form)throw Error("Client does not support form elicitation.");let J=Q.mode==="form"?Q:{...Q,mode:"form"},Y=await this.request({method:"elicitation/create",params:J},GQ,X);if(Y.action==="accept"&&Y.content&&J.requestedSchema)try{let W=this._jsonSchemaValidator.getValidator(J.requestedSchema)(Y.content);if(!W.valid)throw new g(f.InvalidParams,`Elicitation response content does not match requested schema: ${W.errorMessage}`)}catch(G){if(G instanceof g)throw G;throw new g(f.InternalError,`Error validating elicitation response: ${G instanceof Error?G.message:String(G)}`)}return Y}}}createElicitationCompletionNotifier(Q,X){if(!this._clientCapabilities?.elicitation?.url)throw Error("Client does not support URL elicitation (required for notifications/elicitation/complete)");return()=>this.notification({method:"notifications/elicitation/complete",params:{elicitationId:Q}},X)}async listRoots(Q,X){return this.request({method:"roots/list",params:Q},aJ,X)}async sendLoggingMessage(Q,X){if(this._capabilities.logging){if(!this.isMessageIgnored(Q.level,X))return this.notification({method:"notifications/message",params:Q})}}async sendResourceUpdated(Q){return this.notification({method:"notifications/resources/updated",params:Q})}async sendResourceListChanged(){return this.notification({method:"notifications/resources/list_changed"})}async sendToolListChanged(){return this.notification({method:"notifications/tools/list_changed"})}async sendPromptListChanged(){return this.notification({method:"notifications/prompts/list_changed"})}}var K1=Symbol.for("mcp.completable");function UY(Q){return!!Q&&typeof Q==="object"&&K1 in Q}function q1(Q){return Q[K1]?.complete}var z1;(function(Q){Q.Completable="McpCompletable"})(z1||(z1={}));var EM=/^[A-Za-z0-9._-]{1,128}$/;function MM(Q){let X=[];if(Q.length===0)return{isValid:!1,warnings:["Tool name cannot be empty"]};if(Q.length>128)return{isValid:!1,warnings:[`Tool name exceeds maximum length of 128 characters (current: ${Q.length})`]};if(Q.includes(" "))X.push("Tool name contains spaces, which may cause parsing issues");if(Q.includes(","))X.push("Tool name contains commas, which may cause parsing issues");if(Q.startsWith("-")||Q.endsWith("-"))X.push("Tool name starts or ends with a dash, which may cause parsing issues in some contexts");if(Q.startsWith(".")||Q.endsWith("."))X.push("Tool name starts or ends with a dot, which may cause parsing issues in some contexts");if(!EM.test(Q)){let $=Q.split("").filter((J)=>!/[A-Za-z0-9._-]/.test(J)).filter((J,Y,G)=>G.indexOf(J)===Y);return X.push(`Tool name contains invalid characters: ${$.map((J)=>`"${J}"`).join(", ")}`,"Allowed characters are: A-Z, a-z, 0-9, underscore (_), dash (-), and dot (.)"),{isValid:!1,warnings:X}}return{isValid:!0,warnings:X}}function RM(Q,X){if(X.length>0){console.warn(`Tool name validation warning for "${Q}":`);for(let $ of X)console.warn(` - ${$}`);console.warn("Tool registration will proceed, but this may cause compatibility issues."),console.warn("Consider updating the tool name to conform to the MCP tool naming standard."),console.warn("See SEP: Specify Format for Tool Names (https://github.com/modelcontextprotocol/modelcontextprotocol/issues/986) for more details.")}}function AY(Q){let X=MM(Q);return RM(Q,X.warnings),X.isValid}class DY{constructor(Q){this._mcpServer=Q}registerToolTask(Q,X,$){let J={taskSupport:"required",...X.execution};if(J.taskSupport==="forbidden")throw Error(`Cannot register task-based tool '${Q}' with taskSupport 'forbidden'. Use registerTool() instead.`);return this._mcpServer._createRegisteredTool(Q,X.title,X.description,X.inputSchema,X.outputSchema,X.annotations,J,X._meta,$)}}class OY{constructor(Q,X){this._registeredResources={},this._registeredResourceTemplates={},this._registeredTools={},this._registeredPrompts={},this._toolHandlersInitialized=!1,this._completionHandlerInitialized=!1,this._resourceHandlersInitialized=!1,this._promptHandlersInitialized=!1,this.server=new VY(Q,X)}get experimental(){if(!this._experimental)this._experimental={tasks:new DY(this)};return this._experimental}async connect(Q){return await this.server.connect(Q)}async close(){await this.server.close()}setToolRequestHandlers(){if(this._toolHandlersInitialized)return;this.server.assertCanSetRequestHandler(U9(wX)),this.server.assertCanSetRequestHandler(U9(YQ)),this.server.registerCapabilities({tools:{listChanged:!0}}),this.server.setRequestHandler(wX,()=>({tools:Object.entries(this._registeredTools).filter(([,Q])=>Q.enabled).map(([Q,X])=>{let $={name:Q,title:X.title,description:X.description,inputSchema:(()=>{let J=QQ(X.inputSchema);return J?J7(J,{strictUnions:!0,pipeStrategy:"input"}):SM})(),annotations:X.annotations,execution:X.execution,_meta:X._meta};if(X.outputSchema){let J=QQ(X.outputSchema);if(J)$.outputSchema=J7(J,{strictUnions:!0,pipeStrategy:"output"})}return $})})),this.server.setRequestHandler(YQ,async(Q,X)=>{try{let $=this._registeredTools[Q.params.name];if(!$)throw new g(f.InvalidParams,`Tool ${Q.params.name} not found`);if(!$.enabled)throw new g(f.InvalidParams,`Tool ${Q.params.name} disabled`);let J=!!Q.params.task,Y=$.execution?.taskSupport,G="createTask"in $.handler;if((Y==="required"||Y==="optional")&&!G)throw new g(f.InternalError,`Tool ${Q.params.name} has taskSupport '${Y}' but was not registered with registerToolTask`);if(Y==="required"&&!J)throw new g(f.MethodNotFound,`Tool ${Q.params.name} requires task augmentation (taskSupport: 'required')`);if(Y==="optional"&&!J&&G)return await this.handleAutomaticTaskPolling($,Q,X);let W=await this.validateToolInput($,Q.params.arguments,Q.params.name),H=await this.executeToolHandler($,W,X);if(J)return H;return await this.validateToolOutput($,H,Q.params.name),H}catch($){if($ instanceof g){if($.code===f.UrlElicitationRequired)throw $}return this.createToolError($ instanceof Error?$.message:String($))}}),this._toolHandlersInitialized=!0}createToolError(Q){return{content:[{type:"text",text:Q}],isError:!0}}async validateToolInput(Q,X,$){if(!Q.inputSchema)return;let Y=QQ(Q.inputSchema)??Q.inputSchema,G=await t8(Y,X);if(!G.success){let W="error"in G?G.error:"Unknown error",H=a8(W);throw new g(f.InvalidParams,`Input validation error: Invalid arguments for tool ${$}: ${H}`)}return G.data}async validateToolOutput(Q,X,$){if(!Q.outputSchema)return;if(!("content"in X))return;if(X.isError)return;if(!X.structuredContent)throw new g(f.InvalidParams,`Output validation error: Tool ${$} has an output schema but no structured content was provided`);let J=QQ(Q.outputSchema),Y=await t8(J,X.structuredContent);if(!Y.success){let G="error"in Y?Y.error:"Unknown error",W=a8(G);throw new g(f.InvalidParams,`Output validation error: Invalid structured content for tool ${$}: ${W}`)}}async executeToolHandler(Q,X,$){let J=Q.handler;if("createTask"in J){if(!$.taskStore)throw Error("No task store provided.");let G={...$,taskStore:$.taskStore};if(Q.inputSchema)return await Promise.resolve(J.createTask(X,G));else return await Promise.resolve(J.createTask(G))}if(Q.inputSchema)return await Promise.resolve(J(X,$));else return await Promise.resolve(J($))}async handleAutomaticTaskPolling(Q,X,$){if(!$.taskStore)throw Error("No task store provided for task-capable tool.");let J=await this.validateToolInput(Q,X.params.arguments,X.params.name),Y=Q.handler,G={...$,taskStore:$.taskStore},W=J?await Promise.resolve(Y.createTask(J,G)):await Promise.resolve(Y.createTask(G)),H=W.task.taskId,z=W.task,K=z.pollInterval??5000;while(z.status!=="completed"&&z.status!=="failed"&&z.status!=="cancelled"){await new Promise((B)=>setTimeout(B,K));let q=await $.taskStore.getTask(H);if(!q)throw new g(f.InternalError,`Task ${H} not found during polling`);z=q}return await $.taskStore.getTaskResult(H)}setCompletionRequestHandler(){if(this._completionHandlerInitialized)return;this.server.assertCanSetRequestHandler(U9(NX)),this.server.registerCapabilities({completions:{}}),this.server.setRequestHandler(NX,async(Q)=>{switch(Q.params.ref.type){case"ref/prompt":return iG(Q),this.handlePromptCompletion(Q,Q.params.ref);case"ref/resource":return nG(Q),this.handleResourceCompletion(Q,Q.params.ref);default:throw new g(f.InvalidParams,`Invalid completion reference: ${Q.params.ref}`)}}),this._completionHandlerInitialized=!0}async handlePromptCompletion(Q,X){let $=this._registeredPrompts[X.name];if(!$)throw new g(f.InvalidParams,`Prompt ${X.name} not found`);if(!$.enabled)throw new g(f.InvalidParams,`Prompt ${X.name} disabled`);if(!$.argsSchema)return S8;let Y=J9($.argsSchema)?.[Q.params.argument.name];if(!UY(Y))return S8;let G=q1(Y);if(!G)return S8;let W=await G(Q.params.argument.value,Q.params.context);return V1(W)}async handleResourceCompletion(Q,X){let $=Object.values(this._registeredResourceTemplates).find((G)=>G.resourceTemplate.uriTemplate.toString()===X.uri);if(!$){if(this._registeredResources[X.uri])return S8;throw new g(f.InvalidParams,`Resource template ${Q.params.ref.uri} not found`)}let J=$.resourceTemplate.completeCallback(Q.params.argument.name);if(!J)return S8;let Y=await J(Q.params.argument.value,Q.params.context);return V1(Y)}setResourceRequestHandlers(){if(this._resourceHandlersInitialized)return;this.server.assertCanSetRequestHandler(U9(AX)),this.server.assertCanSetRequestHandler(U9(DX)),this.server.assertCanSetRequestHandler(U9(FX)),this.server.registerCapabilities({resources:{listChanged:!0}}),this.server.setRequestHandler(AX,async(Q,X)=>{let $=Object.entries(this._registeredResources).filter(([Y,G])=>G.enabled).map(([Y,G])=>({uri:Y,name:G.name,...G.metadata})),J=[];for(let Y of Object.values(this._registeredResourceTemplates)){if(!Y.resourceTemplate.listCallback)continue;let G=await Y.resourceTemplate.listCallback(X);for(let W of G.resources)J.push({...Y.metadata,...W})}return{resources:[...$,...J]}}),this.server.setRequestHandler(DX,async()=>{return{resourceTemplates:Object.entries(this._registeredResourceTemplates).map(([X,$])=>({name:X,uriTemplate:$.resourceTemplate.uriTemplate.toString(),...$.metadata}))}}),this.server.setRequestHandler(FX,async(Q,X)=>{let $=new URL(Q.params.uri),J=this._registeredResources[$.toString()];if(J){if(!J.enabled)throw new g(f.InvalidParams,`Resource ${$} disabled`);return J.readCallback($,X)}for(let Y of Object.values(this._registeredResourceTemplates)){let G=Y.resourceTemplate.uriTemplate.match($.toString());if(G)return Y.readCallback($,G,X)}throw new g(f.InvalidParams,`Resource ${$} not found`)}),this._resourceHandlersInitialized=!0}setPromptRequestHandlers(){if(this._promptHandlersInitialized)return;this.server.assertCanSetRequestHandler(U9(OX)),this.server.assertCanSetRequestHandler(U9(LX)),this.server.registerCapabilities({prompts:{listChanged:!0}}),this.server.setRequestHandler(OX,()=>({prompts:Object.entries(this._registeredPrompts).filter(([,Q])=>Q.enabled).map(([Q,X])=>{return{name:Q,title:X.title,description:X.description,arguments:X.argsSchema?bM(X.argsSchema):void 0}})})),this.server.setRequestHandler(LX,async(Q,X)=>{let $=this._registeredPrompts[Q.params.name];if(!$)throw new g(f.InvalidParams,`Prompt ${Q.params.name} not found`);if(!$.enabled)throw new g(f.InvalidParams,`Prompt ${Q.params.name} disabled`);if($.argsSchema){let J=QQ($.argsSchema),Y=await t8(J,Q.params.arguments);if(!Y.success){let H="error"in Y?Y.error:"Unknown error",z=a8(H);throw new g(f.InvalidParams,`Invalid arguments for prompt ${Q.params.name}: ${z}`)}let G=Y.data,W=$.callback;return await Promise.resolve(W(G,X))}else{let J=$.callback;return await Promise.resolve(J(X))}}),this._promptHandlersInitialized=!0}resource(Q,X,...$){let J;if(typeof $[0]==="object")J=$.shift();let Y=$[0];if(typeof X==="string"){if(this._registeredResources[X])throw Error(`Resource ${X} is already registered`);let G=this._createRegisteredResource(Q,void 0,X,J,Y);return this.setResourceRequestHandlers(),this.sendResourceListChanged(),G}else{if(this._registeredResourceTemplates[Q])throw Error(`Resource template ${Q} is already registered`);let G=this._createRegisteredResourceTemplate(Q,void 0,X,J,Y);return this.setResourceRequestHandlers(),this.sendResourceListChanged(),G}}registerResource(Q,X,$,J){if(typeof X==="string"){if(this._registeredResources[X])throw Error(`Resource ${X} is already registered`);let Y=this._createRegisteredResource(Q,$.title,X,$,J);return this.setResourceRequestHandlers(),this.sendResourceListChanged(),Y}else{if(this._registeredResourceTemplates[Q])throw Error(`Resource template ${Q} is already registered`);let Y=this._createRegisteredResourceTemplate(Q,$.title,X,$,J);return this.setResourceRequestHandlers(),this.sendResourceListChanged(),Y}}_createRegisteredResource(Q,X,$,J,Y){let G={name:Q,title:X,metadata:J,readCallback:Y,enabled:!0,disable:()=>G.update({enabled:!1}),enable:()=>G.update({enabled:!0}),remove:()=>G.update({uri:null}),update:(W)=>{if(typeof W.uri<"u"&&W.uri!==$){if(delete this._registeredResources[$],W.uri)this._registeredResources[W.uri]=G}if(typeof W.name<"u")G.name=W.name;if(typeof W.title<"u")G.title=W.title;if(typeof W.metadata<"u")G.metadata=W.metadata;if(typeof W.callback<"u")G.readCallback=W.callback;if(typeof W.enabled<"u")G.enabled=W.enabled;this.sendResourceListChanged()}};return this._registeredResources[$]=G,G}_createRegisteredResourceTemplate(Q,X,$,J,Y){let G={resourceTemplate:$,title:X,metadata:J,readCallback:Y,enabled:!0,disable:()=>G.update({enabled:!1}),enable:()=>G.update({enabled:!0}),remove:()=>G.update({name:null}),update:(z)=>{if(typeof z.name<"u"&&z.name!==Q){if(delete this._registeredResourceTemplates[Q],z.name)this._registeredResourceTemplates[z.name]=G}if(typeof z.title<"u")G.title=z.title;if(typeof z.template<"u")G.resourceTemplate=z.template;if(typeof z.metadata<"u")G.metadata=z.metadata;if(typeof z.callback<"u")G.readCallback=z.callback;if(typeof z.enabled<"u")G.enabled=z.enabled;this.sendResourceListChanged()}};this._registeredResourceTemplates[Q]=G;let W=$.uriTemplate.variableNames;if(Array.isArray(W)&&W.some((z)=>!!$.completeCallback(z)))this.setCompletionRequestHandler();return G}_createRegisteredPrompt(Q,X,$,J,Y){let G={title:X,description:$,argsSchema:J===void 0?void 0:I9(J),callback:Y,enabled:!0,disable:()=>G.update({enabled:!1}),enable:()=>G.update({enabled:!0}),remove:()=>G.update({name:null}),update:(W)=>{if(typeof W.name<"u"&&W.name!==Q){if(delete this._registeredPrompts[Q],W.name)this._registeredPrompts[W.name]=G}if(typeof W.title<"u")G.title=W.title;if(typeof W.description<"u")G.description=W.description;if(typeof W.argsSchema<"u")G.argsSchema=I9(W.argsSchema);if(typeof W.callback<"u")G.callback=W.callback;if(typeof W.enabled<"u")G.enabled=W.enabled;this.sendPromptListChanged()}};if(this._registeredPrompts[Q]=G,J){if(Object.values(J).some((H)=>{let z=H instanceof $6?H._def?.innerType:H;return UY(z)}))this.setCompletionRequestHandler()}return G}_createRegisteredTool(Q,X,$,J,Y,G,W,H,z){AY(Q);let K={title:X,description:$,inputSchema:B1(J),outputSchema:B1(Y),annotations:G,execution:W,_meta:H,handler:z,enabled:!0,disable:()=>K.update({enabled:!1}),enable:()=>K.update({enabled:!0}),remove:()=>K.update({name:null}),update:(q)=>{if(typeof q.name<"u"&&q.name!==Q){if(typeof q.name==="string")AY(q.name);if(delete this._registeredTools[Q],q.name)this._registeredTools[q.name]=K}if(typeof q.title<"u")K.title=q.title;if(typeof q.description<"u")K.description=q.description;if(typeof q.paramsSchema<"u")K.inputSchema=I9(q.paramsSchema);if(typeof q.outputSchema<"u")K.outputSchema=I9(q.outputSchema);if(typeof q.callback<"u")K.handler=q.callback;if(typeof q.annotations<"u")K.annotations=q.annotations;if(typeof q._meta<"u")K._meta=q._meta;if(typeof q.enabled<"u")K.enabled=q.enabled;this.sendToolListChanged()}};return this._registeredTools[Q]=K,this.setToolRequestHandlers(),this.sendToolListChanged(),K}tool(Q,...X){if(this._registeredTools[Q])throw Error(`Tool ${Q} is already registered`);let $,J,Y,G;if(typeof X[0]==="string")$=X.shift();if(X.length>1){let H=X[0];if(FY(H)){if(J=X.shift(),X.length>1&&typeof X[0]==="object"&&X[0]!==null&&!FY(X[0]))G=X.shift()}else if(typeof H==="object"&&H!==null)G=X.shift()}let W=X[0];return this._createRegisteredTool(Q,void 0,$,J,Y,G,{taskSupport:"forbidden"},void 0,W)}registerTool(Q,X,$){if(this._registeredTools[Q])throw Error(`Tool ${Q} is already registered`);let{title:J,description:Y,inputSchema:G,outputSchema:W,annotations:H,_meta:z}=X;return this._createRegisteredTool(Q,J,Y,G,W,H,{taskSupport:"forbidden"},z,$)}prompt(Q,...X){if(this._registeredPrompts[Q])throw Error(`Prompt ${Q} is already registered`);let $;if(typeof X[0]==="string")$=X.shift();let J;if(X.length>1)J=X.shift();let Y=X[0],G=this._createRegisteredPrompt(Q,void 0,$,J,Y);return this.setPromptRequestHandlers(),this.sendPromptListChanged(),G}registerPrompt(Q,X,$){if(this._registeredPrompts[Q])throw Error(`Prompt ${Q} is already registered`);let{title:J,description:Y,argsSchema:G}=X,W=this._createRegisteredPrompt(Q,J,Y,G,$);return this.setPromptRequestHandlers(),this.sendPromptListChanged(),W}isConnected(){return this.server.transport!==void 0}async sendLoggingMessage(Q,X){return this.server.sendLoggingMessage(Q,X)}sendResourceListChanged(){if(this.isConnected())this.server.sendResourceListChanged()}sendToolListChanged(){if(this.isConnected())this.server.sendToolListChanged()}sendPromptListChanged(){if(this.isConnected())this.server.sendPromptListChanged()}}var SM={type:"object",properties:{}};function U1(Q){return Q!==null&&typeof Q==="object"&&"parse"in Q&&typeof Q.parse==="function"&&"safeParse"in Q&&typeof Q.safeParse==="function"}function PM(Q){return"_def"in Q||"_zod"in Q||U1(Q)}function FY(Q){if(typeof Q!=="object"||Q===null)return!1;if(PM(Q))return!1;if(Object.keys(Q).length===0)return!0;return Object.values(Q).some(U1)}function B1(Q){if(!Q)return;if(FY(Q))return I9(Q);return Q}function bM(Q){let X=J9(Q);if(!X)return[];return Object.entries(X).map(([$,J])=>{let Y=WG(J),G=HG(J);return{name:$,description:Y,required:!G}})}function U9(Q){let $=J9(Q)?.method;if(!$)throw Error("Schema is missing a method literal");let J=s8($);if(typeof J==="string")return J;throw Error("Schema method literal must be a string")}function V1(Q){return{completion:{values:Q.slice(0,100),total:Q.length,hasMore:Q.length>100}}}var S8={completion:{values:[],hasMore:!1}};import D1 from"node:process";class LY{append(Q){this._buffer=this._buffer?Buffer.concat([this._buffer,Q]):Q}readMessage(){if(!this._buffer)return null;let Q=this._buffer.indexOf(`
44
44
  `);if(Q===-1)return null;let X=this._buffer.toString("utf8",0,Q).replace(/\r$/,"");return this._buffer=this._buffer.subarray(Q+1),IM(X)}clear(){this._buffer=void 0}}function IM(Q){return yG.parse(JSON.parse(Q))}function A1(Q){return JSON.stringify(Q)+`
45
- `}class wY{constructor(Q=D1.stdin,X=D1.stdout){this._stdin=Q,this._stdout=X,this._readBuffer=new LY,this._started=!1,this._ondata=($)=>{this._readBuffer.append($),this.processReadBuffer()},this._onerror=($)=>{this.onerror?.($)}}async start(){if(this._started)throw Error("StdioServerTransport already started! If using Server class, note that connect() calls start() automatically.");this._started=!0,this._stdin.on("data",this._ondata),this._stdin.on("error",this._onerror)}processReadBuffer(){while(!0)try{let Q=this._readBuffer.readMessage();if(Q===null)break;this.onmessage?.(Q)}catch(Q){this.onerror?.(Q)}}async close(){if(this._stdin.off("data",this._ondata),this._stdin.off("error",this._onerror),this._stdin.listenerCount("data")===0)this._stdin.pause();this._readBuffer.clear(),this.onclose?.()}send(Q){return new Promise((X)=>{let $=A1(Q);if(this._stdout.write($))X();else this._stdout.once("drain",X)})}}var F1={name:"trueline-mcp",version:"2.5.9",type:"module",description:"Truth-verified file editing for AI coding agents via MCP",license:"Apache-2.0",repository:{type:"git",url:"https://github.com/rjkaes/trueline-mcp.git"},homepage:"https://github.com/rjkaes/trueline-mcp#readme",bugs:{url:"https://github.com/rjkaes/trueline-mcp/issues"},keywords:["mcp","trueline","editing","verification","claude-code","gemini-cli","copilot"],bin:{"trueline-mcp":"./bin/trueline-mcp.js","trueline-hook":"./bin/trueline-hook.js"},files:["dist/","bin/","scripts/resolve-binary.cjs","hooks/","configs/","src/security.js","README.md","INSTALL.md","LICENSE"],scripts:{dev:"bun run src/server.ts",build:"bun build src/server.ts --target=node --minify --outfile dist/server.js",prepublishOnly:"bun run build",typecheck:"bun x tsc --noEmit",test:"bun test",lint:"bunx biome ci .","lint:fix":"bunx biome check --write .",benchmark:"bun run benchmarks/token-benchmark.ts && bun run benchmarks/perf-benchmark.ts"},dependencies:{"@modelcontextprotocol/sdk":"^1.26.0","tree-sitter-wasms":"0.1.13","web-tree-sitter":"0.24.7",zod:"^3.25.0"},devDependencies:{"@biomejs/biome":"2.4.5","@types/node":"^22.19.0","@xmldom/xmldom":"^0.8.11","bun-types":"^1.3.10",lefthook:"^2.1.2",typescript:"^5.7.0"}};function b0(Q){return{content:[{type:"text",text:Q}]}}function G0(Q){return{content:[{type:"text",text:Q}],isError:!0}}import{execSync as WR}from"node:child_process";import{readFile as HR}from"node:fs/promises";import{extname as zR,relative as p1,resolve as KR}from"node:path";var M1={grammar:"typescript",outline:new Set(["function_declaration","class_declaration","interface_declaration","type_alias_declaration","enum_declaration","lexical_declaration","variable_declaration","export_statement","method_definition","public_field_definition"]),skip:new Set(["import_statement"]),recurse:new Set(["class_body"]),topLevelOnly:new Set(["expression_statement"])},CM={...M1,grammar:"tsx"},K4={grammar:"javascript",outline:new Set(["function_declaration","class_declaration","lexical_declaration","variable_declaration","export_statement","expression_statement","method_definition","field_definition"]),skip:new Set(["import_statement"]),recurse:new Set(["class_body"])},O1={grammar:"python",outline:new Set(["function_definition","class_definition","decorated_definition","expression_statement"]),skip:new Set(["import_statement","import_from_statement"]),recurse:new Set(["block"]),whitespaceMode:"preserve-indent"},TM={grammar:"go",outline:new Set(["function_declaration","method_declaration","type_declaration","const_declaration","var_declaration"]),skip:new Set(["package_clause","import_declaration"]),recurse:new Set([])},kM={grammar:"rust",outline:new Set(["function_item","struct_item","enum_item","trait_item","impl_item","const_item","static_item","mod_item","type_item","macro_definition"]),skip:new Set(["use_declaration"]),recurse:new Set(["declaration_list"])},vM={grammar:"java",outline:new Set(["class_declaration","interface_declaration","enum_declaration","method_declaration","constructor_declaration","field_declaration"]),skip:new Set(["package_declaration","import_declaration"]),recurse:new Set(["class_body"])},_M={grammar:"ruby",outline:new Set(["method","class","module","assignment","call"]),skip:new Set([]),recurse:new Set(["body_statement"])},P8={grammar:"cpp",outline:new Set(["function_definition","class_specifier","struct_specifier","enum_specifier","namespace_definition","declaration","template_declaration"]),skip:new Set(["preproc_include"]),recurse:new Set(["declaration_list","field_declaration_list"])},L1={grammar:"c",outline:new Set(["function_definition","struct_specifier","enum_specifier","declaration","type_definition"]),skip:new Set(["preproc_include"]),recurse:new Set([])},xM={grammar:"c_sharp",outline:new Set(["class_declaration","interface_declaration","struct_declaration","enum_declaration","method_declaration","constructor_declaration","property_declaration","namespace_declaration"]),skip:new Set(["using_directive"]),recurse:new Set(["declaration_list"])},w1={grammar:"kotlin",outline:new Set(["function_declaration","class_declaration","object_declaration","property_declaration"]),skip:new Set(["import_list","package_header"]),recurse:new Set(["class_body"])},gM={grammar:"swift",outline:new Set(["function_declaration","class_declaration","struct_declaration","enum_declaration","protocol_declaration","extension_declaration","property_declaration"]),skip:new Set(["import_declaration"]),recurse:new Set(["class_body"])},yM={grammar:"php",outline:new Set(["function_definition","class_declaration","interface_declaration","trait_declaration","method_declaration","property_declaration"]),skip:new Set(["namespace_use_declaration"]),recurse:new Set(["declaration_list"])},j1={grammar:"scala",outline:new Set(["function_definition","class_definition","object_definition","trait_definition","val_definition","var_definition","type_definition"]),skip:new Set(["import_declaration"]),recurse:new Set(["template_body"])},N1={grammar:"elixir",outline:new Set(["call"]),skip:new Set([]),recurse:new Set([])},hM={grammar:"lua",outline:new Set(["function_declaration","local_function","variable_declaration","local_variable_declaration","assignment_statement"]),skip:new Set([]),recurse:new Set([])},fM={grammar:"dart",outline:new Set(["function_signature","class_definition","enum_declaration","mixin_declaration","extension_declaration","type_alias"]),skip:new Set(["import_or_export"]),recurse:new Set(["class_body"])},uM={grammar:"zig",outline:new Set(["TopLevelDecl","VarDecl","FnProto"]),skip:new Set([]),recurse:new Set([])},E1={grammar:"bash",outline:new Set(["function_definition","variable_assignment"]),skip:new Set([]),recurse:new Set([])},mM={".ts":M1,".tsx":CM,".js":K4,".jsx":K4,".mjs":K4,".cjs":K4,".py":O1,".pyi":O1,".go":TM,".rs":kM,".java":vM,".c":L1,".h":L1,".cpp":P8,".cc":P8,".cxx":P8,".hpp":P8,".hh":P8,".cs":xM,".rb":_M,".php":yM,".kt":w1,".kts":w1,".swift":gM,".scala":j1,".sc":j1,".ex":N1,".exs":N1,".lua":hM,".dart":fM,".zig":uM,".sh":E1,".bash":E1};function LQ(Q){return mM[Q]}var B4=N4(S1(),1);import{createRequire as pM}from"node:module";import{resolve as I1,dirname as Z1}from"node:path";var C1=pM(import.meta.url),P1=!1,b1=new Map;function iM(){let Q=C1.resolve("web-tree-sitter/package.json");return I1(Z1(Q),"tree-sitter.wasm")}async function T1(){if(P1)return;let Q=new Promise((X,$)=>setTimeout(()=>$(Error("tree-sitter WASM init timed out after 10 s")),1e4));await Promise.race([B4.default.init({locateFile:()=>iM()}),Q]),P1=!0}function nM(Q){let X=C1.resolve("tree-sitter-wasms/package.json");return I1(Z1(X),"out",`tree-sitter-${Q}.wasm`)}async function dM(Q){let X=b1.get(Q);if(X)return X;await T1();let $=await B4.default.Language.load(nM(Q));return b1.set(Q,$),$}async function k1(Q){await T1();let X=new B4.default,$=await dM(Q);return X.setLanguage($),X}async function V4(Q,X,$=1/0){let Y=(await k1(X.grammar)).parse(Q),G=Q.split(`
45
+ `}class wY{constructor(Q=D1.stdin,X=D1.stdout){this._stdin=Q,this._stdout=X,this._readBuffer=new LY,this._started=!1,this._ondata=($)=>{this._readBuffer.append($),this.processReadBuffer()},this._onerror=($)=>{this.onerror?.($)}}async start(){if(this._started)throw Error("StdioServerTransport already started! If using Server class, note that connect() calls start() automatically.");this._started=!0,this._stdin.on("data",this._ondata),this._stdin.on("error",this._onerror)}processReadBuffer(){while(!0)try{let Q=this._readBuffer.readMessage();if(Q===null)break;this.onmessage?.(Q)}catch(Q){this.onerror?.(Q)}}async close(){if(this._stdin.off("data",this._ondata),this._stdin.off("error",this._onerror),this._stdin.listenerCount("data")===0)this._stdin.pause();this._readBuffer.clear(),this.onclose?.()}send(Q){return new Promise((X)=>{let $=A1(Q);if(this._stdout.write($))X();else this._stdout.once("drain",X)})}}var F1={name:"trueline-mcp",version:"2.6.0",type:"module",description:"Truth-verified file editing for AI coding agents via MCP",license:"Apache-2.0",repository:{type:"git",url:"https://github.com/rjkaes/trueline-mcp.git"},homepage:"https://github.com/rjkaes/trueline-mcp#readme",bugs:{url:"https://github.com/rjkaes/trueline-mcp/issues"},keywords:["mcp","trueline","editing","verification","claude-code","gemini-cli","copilot"],bin:{"trueline-mcp":"./bin/trueline-mcp.js","trueline-hook":"./bin/trueline-hook.js"},files:["dist/","bin/","scripts/resolve-binary.cjs","hooks/","configs/","src/security.js","README.md","INSTALL.md","LICENSE"],scripts:{dev:"bun run src/server.ts",build:"bun build src/server.ts --target=node --minify --outfile dist/server.js",prepublishOnly:"bun run build",typecheck:"bun x tsc --noEmit",test:"bun test",lint:"bunx biome ci .","lint:fix":"bunx biome check --write .",benchmark:"bun run benchmarks/token-benchmark.ts && bun run benchmarks/perf-benchmark.ts"},dependencies:{"@modelcontextprotocol/sdk":"^1.26.0","tree-sitter-wasms":"0.1.13","web-tree-sitter":"0.24.7",zod:"^3.25.0"},devDependencies:{"@biomejs/biome":"2.4.5","@types/node":"^22.19.0","@xmldom/xmldom":"^0.8.11","bun-types":"^1.3.10",lefthook:"^2.1.2",typescript:"^5.7.0"}};function b0(Q){return{content:[{type:"text",text:Q}]}}function G0(Q){return{content:[{type:"text",text:Q}],isError:!0}}import{execSync as WR}from"node:child_process";import{readFile as HR}from"node:fs/promises";import{extname as zR,relative as p1,resolve as KR}from"node:path";var M1={grammar:"typescript",outline:new Set(["function_declaration","class_declaration","interface_declaration","type_alias_declaration","enum_declaration","lexical_declaration","variable_declaration","export_statement","method_definition","public_field_definition"]),skip:new Set(["import_statement"]),recurse:new Set(["class_body"]),topLevelOnly:new Set(["expression_statement"])},CM={...M1,grammar:"tsx"},K4={grammar:"javascript",outline:new Set(["function_declaration","class_declaration","lexical_declaration","variable_declaration","export_statement","expression_statement","method_definition","field_definition"]),skip:new Set(["import_statement"]),recurse:new Set(["class_body"])},O1={grammar:"python",outline:new Set(["function_definition","class_definition","decorated_definition","expression_statement"]),skip:new Set(["import_statement","import_from_statement"]),recurse:new Set(["block"]),whitespaceMode:"preserve-indent"},TM={grammar:"go",outline:new Set(["function_declaration","method_declaration","type_declaration","const_declaration","var_declaration"]),skip:new Set(["package_clause","import_declaration"]),recurse:new Set([])},kM={grammar:"rust",outline:new Set(["function_item","struct_item","enum_item","trait_item","impl_item","const_item","static_item","mod_item","type_item","macro_definition"]),skip:new Set(["use_declaration"]),recurse:new Set(["declaration_list"])},vM={grammar:"java",outline:new Set(["class_declaration","interface_declaration","enum_declaration","method_declaration","constructor_declaration","field_declaration"]),skip:new Set(["package_declaration","import_declaration"]),recurse:new Set(["class_body"])},_M={grammar:"ruby",outline:new Set(["method","class","module","assignment","call"]),skip:new Set([]),recurse:new Set(["body_statement"])},P8={grammar:"cpp",outline:new Set(["function_definition","class_specifier","struct_specifier","enum_specifier","namespace_definition","declaration","template_declaration"]),skip:new Set(["preproc_include"]),recurse:new Set(["declaration_list","field_declaration_list"])},L1={grammar:"c",outline:new Set(["function_definition","struct_specifier","enum_specifier","declaration","type_definition"]),skip:new Set(["preproc_include"]),recurse:new Set([])},xM={grammar:"c_sharp",outline:new Set(["class_declaration","interface_declaration","struct_declaration","enum_declaration","method_declaration","constructor_declaration","property_declaration","namespace_declaration"]),skip:new Set(["using_directive"]),recurse:new Set(["declaration_list"])},w1={grammar:"kotlin",outline:new Set(["function_declaration","class_declaration","object_declaration","property_declaration"]),skip:new Set(["import_list","package_header"]),recurse:new Set(["class_body"])},gM={grammar:"swift",outline:new Set(["function_declaration","class_declaration","struct_declaration","enum_declaration","protocol_declaration","extension_declaration","property_declaration"]),skip:new Set(["import_declaration"]),recurse:new Set(["class_body"])},yM={grammar:"php",outline:new Set(["function_definition","class_declaration","interface_declaration","trait_declaration","method_declaration","property_declaration"]),skip:new Set(["namespace_use_declaration"]),recurse:new Set(["declaration_list"])},j1={grammar:"scala",outline:new Set(["function_definition","class_definition","object_definition","trait_definition","val_definition","var_definition","type_definition"]),skip:new Set(["import_declaration"]),recurse:new Set(["template_body"])},N1={grammar:"elixir",outline:new Set(["call"]),skip:new Set([]),recurse:new Set([])},hM={grammar:"lua",outline:new Set(["function_declaration","local_function","variable_declaration","local_variable_declaration","assignment_statement"]),skip:new Set([]),recurse:new Set([])},fM={grammar:"dart",outline:new Set(["function_signature","class_definition","enum_declaration","mixin_declaration","extension_declaration","type_alias"]),skip:new Set(["import_or_export"]),recurse:new Set(["class_body"])},uM={grammar:"zig",outline:new Set(["TopLevelDecl","VarDecl","FnProto"]),skip:new Set([]),recurse:new Set([])},E1={grammar:"bash",outline:new Set(["function_definition","variable_assignment"]),skip:new Set([]),recurse:new Set([])},mM={".ts":M1,".tsx":CM,".js":K4,".jsx":K4,".mjs":K4,".cjs":K4,".py":O1,".pyi":O1,".go":TM,".rs":kM,".java":vM,".c":L1,".h":L1,".cpp":P8,".cc":P8,".cxx":P8,".hpp":P8,".hh":P8,".cs":xM,".rb":_M,".php":yM,".kt":w1,".kts":w1,".swift":gM,".scala":j1,".sc":j1,".ex":N1,".exs":N1,".lua":hM,".dart":fM,".zig":uM,".sh":E1,".bash":E1};function LQ(Q){return mM[Q]}var B4=N4(S1(),1);import{createRequire as pM}from"node:module";import{resolve as I1,dirname as Z1}from"node:path";var C1=pM(import.meta.url),P1=!1,b1=new Map;function iM(){let Q=C1.resolve("web-tree-sitter/package.json");return I1(Z1(Q),"tree-sitter.wasm")}async function T1(){if(P1)return;let Q=new Promise((X,$)=>setTimeout(()=>$(Error("tree-sitter WASM init timed out after 10 s")),1e4));await Promise.race([B4.default.init({locateFile:()=>iM()}),Q]),P1=!0}function nM(Q){let X=C1.resolve("tree-sitter-wasms/package.json");return I1(Z1(X),"out",`tree-sitter-${Q}.wasm`)}async function dM(Q){let X=b1.get(Q);if(X)return X;await T1();let $=await B4.default.Language.load(nM(Q));return b1.set(Q,$),$}async function k1(Q){await T1();let X=new B4.default,$=await dM(Q);return X.setLanguage($),X}async function V4(Q,X,$=1/0){let Y=(await k1(X.grammar)).parse(Q),G=Q.split(`
46
46
  `),W=[];function H(F){let L=F.startPosition.row,O=F.endPosition.row,w=G[L]?.trimEnd()??"";if(L===O||w.includes("{"))return w.length>200?`${w.slice(0,197)}...`:w;let E=[w.trimEnd()];for(let S=L+1;S<=Math.min(O,L+20);S++){let _=(G[S]??"").trimEnd();if(E.push(_.trim()),_.includes("{"))break}let P=E.join(" ").replace(/\s+/g," ").replace(/\(\s+/g,"(").replace(/,\s*\)/g,")").replace(/\s*\{\s*$/,"");if(P.length>200)P=`${P.slice(0,197)}...`;return P}let z=-1,K=-1,q=0,B="";function A(){if(q===0)return;let F=q===1?`1 ${B}`:`${q} ${B}s`;W.push({startLine:z,endLine:K,depth:0,nodeType:"_skipped",text:`(${F})`}),z=-1,K=-1,q=0,B=""}function U(F){let L=F.startPosition.row+1,O=F.endPosition.row+1,w=F.type.replace(/_/g," ").replace(/ statement$/,"").replace(/ declaration$/,"");if(q===0||w===B){if(q===0)z=L,B=w;K=O,q++}else A(),z=L,K=O,q=1,B=w}function D(F,L,O){if(O&&X.skip.has(F.type)){U(F);return}if(X.skip.has(F.type))return;let w=X.outline.has(F.type),E=X.topLevelOnly?.has(F.type)??!1;if(E&&!O)return;if(w||E&&O){if(O)A();if(W.push({startLine:F.startPosition.row+1,endLine:F.endPosition.row+1,depth:L,nodeType:F.type,text:H(F)}),L+1<=$)for(let P of F.children){if(!P.isNamed)continue;if(X.recurse.has(P.type))for(let S of P.children){if(!S.isNamed)continue;D(S,L+1,!1)}}return}}for(let F of Y.rootNode.children)D(F,0,!0);return A(),W}function v1(Q,X){let $=[];for(let J of Q){let Y=" ".repeat(J.depth);$.push(`${Y}${J.startLine}-${J.endLine}: ${J.text}`)}return $.push(""),$.push(`(${Q.length} symbols, ${X} source lines)`),$.join(`
47
47
  `)}var U6=2166136261;var U4="0-0:00000000";function x1(Q){let X=2166136261;for(let $=0;$<Q.length;$++){let J=Q.charCodeAt($);if(J>=55296&&J<=56319&&$+1<Q.length){let Y=Q.charCodeAt($+1);if(Y>=56320&&Y<=57343)J=(J-55296<<10)+(Y-56320)+65536,$++}if(J<128)X=Math.imul(X^J,16777619)>>>0;else if(J<2048)X=Math.imul(X^(192|J>>6),16777619)>>>0,X=Math.imul(X^(128|J&63),16777619)>>>0;else if(J<65536)X=Math.imul(X^(224|J>>12),16777619)>>>0,X=Math.imul(X^(128|J>>6&63),16777619)>>>0,X=Math.imul(X^(128|J&63),16777619)>>>0;else X=Math.imul(X^(240|J>>18),16777619)>>>0,X=Math.imul(X^(128|J>>12&63),16777619)>>>0,X=Math.imul(X^(128|J>>6&63),16777619)>>>0,X=Math.imul(X^(128|J&63),16777619)>>>0}return X>>>0}function d6(Q,X,$){let J=2166136261;for(let Y=X;Y<$;Y++)J=Math.imul(J^Q[Y],16777619)>>>0;return J}function C6(Q,X){return Q=Math.imul(Q^X&255,16777619)>>>0,Q=Math.imul(Q^X>>>8&255,16777619)>>>0,Q=Math.imul(Q^X>>>16&255,16777619)>>>0,Q=Math.imul(Q^X>>>24&255,16777619)>>>0,Q}function T6(Q,X,$){return`${Q}-${X}:${$.toString(16).padStart(8,"0")}`}var _1="abcdefghijklmnopqrstuvwxyz",oM=(()=>{let Q=Array(676);for(let X=0;X<26;X++)for(let $=0;$<26;$++)Q[X*26+$]=_1[X]+_1[$];return Q})();function jQ(Q){return oM[(Q>>>0)%26*26+(Q>>>8>>>0)%26]}function rM(Q,X="collapse"){let $=Q.split(`
48
48
  `);if(X==="preserve-indent")return $.map((J)=>J.replace(/\s+$/,"").replace(/(\S)\s{2,}(\S)/g,"$1 $2")).join(`
@@ -8,20 +8,24 @@
8
8
  const formatters = {
9
9
  "claude-code": {
10
10
  block: (reason) => ({ decision: "block", reason }),
11
+ advise: (reason) => ({ decision: "approve", reason }),
11
12
  approve: () => ({ decision: "approve" }),
12
13
  },
13
14
  "gemini-cli": {
14
15
  block: (reason) => ({ decision: "deny", reason }),
16
+ advise: (reason) => ({ decision: "allow", reason }),
15
17
  approve: () => null,
16
18
  },
17
19
  "vscode-copilot": {
18
20
  block: (reason) => ({ permissionDecision: "deny", reason }),
21
+ advise: (reason) => ({ permissionDecision: "allow", reason }),
19
22
  approve: () => null,
20
23
  },
21
24
  // OpenCode uses in-process TS plugins, not JSON hooks. Included for
22
25
  // completeness but the CLI dispatcher is the only realistic consumer.
23
26
  opencode: {
24
27
  block: (reason) => ({ decision: "block", reason }),
28
+ advise: (reason) => ({ decision: "approve", reason }),
25
29
  approve: () => null,
26
30
  },
27
31
  };
@@ -30,7 +34,7 @@ const formatters = {
30
34
  * Format a routing decision for a specific platform.
31
35
  *
32
36
  * @param {string} platform
33
- * @param {{ action: "block"; reason: string } | null} routing
37
+ * @param {{ action: "advise" | "block"; reason: string } | null} routing
34
38
  * @returns {Record<string, unknown> | null} JSON to write to stdout, or null for passthrough
35
39
  */
36
40
  export function formatDecision(platform, routing) {
@@ -38,5 +42,6 @@ export function formatDecision(platform, routing) {
38
42
 
39
43
  if (!routing) return fmt.approve();
40
44
  if (routing.action === "block") return fmt.block(routing.reason);
45
+ if (routing.action === "advise") return fmt.advise(routing.reason);
41
46
  return fmt.approve();
42
47
  }
@@ -3,63 +3,43 @@
3
3
  // ==============================================================================
4
4
  //
5
5
  // Generates the <trueline_mcp_instructions> block with platform-specific
6
- // rules about which built-in tools to avoid.
6
+ // tool routing guidance based on file size.
7
7
 
8
- const PLATFORM_RULES = {
8
+ const PLATFORM_TOOLS = {
9
9
  "claude-code": {
10
- editAdvice: "Never use the built-in Edit or MultiEdit tools \u2014 they are blocked and will be rejected.",
11
- readAdvice:
12
- "Never use the built-in Read tool \u2014 use trueline_read instead. " +
13
- "trueline_read returns per-line hashes and checksums needed for trueline_edit.",
14
- writeAdvice:
15
- "Use the built-in Write tool to create new files. " +
16
- "To edit them afterward, use trueline_read or trueline_search to get checksums first.",
10
+ readTool: "Read",
11
+ editTool: "Edit",
12
+ writeTool: "Write",
13
+ grepAdvice: "use Grep to identify the files",
17
14
  atRefAdvice:
18
15
  "If file content was injected by an @ reference, never call Read or trueline_read just to view it again. " +
19
16
  "Only call trueline_read or trueline_search when you need checksums for editing.",
20
- grepAdvice: "use Grep to identify the files",
21
17
  },
22
18
  "gemini-cli": {
23
- editAdvice: "Never use edit_file \u2014 use trueline_edit instead.",
24
- readAdvice:
25
- "Never use read_file or read_many_files \u2014 use trueline_read instead. " +
26
- "trueline_read returns per-line hashes and checksums needed for trueline_edit.",
27
- writeAdvice:
28
- "Use write_file to create new files. " +
29
- "To edit them afterward, use trueline_read or trueline_search to get checksums first.",
19
+ readTool: "read_file",
20
+ editTool: "edit_file",
21
+ writeTool: "write_file",
30
22
  grepAdvice: "use run_shell_command with grep/rg to identify the files",
31
23
  },
32
24
  "vscode-copilot": {
33
- editAdvice: "Never use the built-in Edit or MultiEdit tools \u2014 they are blocked and will be rejected.",
34
- readAdvice:
35
- "Never use the built-in Read tool \u2014 use trueline_read instead. " +
36
- "trueline_read returns per-line hashes and checksums needed for trueline_edit.",
37
- writeAdvice:
38
- "Use the built-in Write tool to create new files. " +
39
- "To edit them afterward, use trueline_read or trueline_search to get checksums first.",
25
+ readTool: "Read",
26
+ editTool: "Edit",
27
+ writeTool: "Write",
28
+ grepAdvice: "use Grep to identify the files",
40
29
  atRefAdvice:
41
30
  "If file content was injected by an @ reference, never call Read or trueline_read just to view it again. " +
42
31
  "Only call trueline_read or trueline_search when you need checksums for editing.",
43
- grepAdvice: "use Grep to identify the files",
44
32
  },
45
33
  opencode: {
46
- editAdvice: "Never use the built-in edit tool \u2014 use trueline_edit instead.",
47
- readAdvice:
48
- "Never use the built-in view tool \u2014 use trueline_read instead. " +
49
- "trueline_read returns per-line hashes and checksums needed for trueline_edit.",
50
- writeAdvice:
51
- "Use the built-in write tool to create new files. " +
52
- "To edit them afterward, use trueline_read or trueline_search to get checksums first.",
34
+ readTool: "view",
35
+ editTool: "edit",
36
+ writeTool: "write",
53
37
  grepAdvice: "use bash with grep/rg to identify the files",
54
38
  },
55
39
  codex: {
56
- editAdvice: "",
57
- readAdvice:
58
- "Never use read_file or shell with cat/head/tail \u2014 use trueline_read instead. " +
59
- "trueline_read returns per-line hashes and checksums needed for trueline_edit.",
60
- writeAdvice:
61
- "Use shell redirection to create new files. " +
62
- "To edit them afterward, use trueline_read or trueline_search to get checksums first.",
40
+ readTool: "read_file / shell cat",
41
+ editTool: "shell sed/awk",
42
+ writeTool: "shell redirection",
63
43
  grepAdvice: "use shell with grep/rg to identify the files",
64
44
  },
65
45
  };
@@ -70,10 +50,9 @@ const PLATFORM_RULES = {
70
50
  * @returns {string}
71
51
  */
72
52
  export function getInstructions(platform = "claude-code") {
73
- const rules = PLATFORM_RULES[platform] ?? PLATFORM_RULES["claude-code"];
53
+ const p = PLATFORM_TOOLS[platform] ?? PLATFORM_TOOLS["claude-code"];
74
54
 
75
- const editRule = rules.editAdvice ? `\n <rule>${rules.editAdvice}</rule>` : "";
76
- const atRefRule = rules.atRefAdvice ? `\n <rule>${rules.atRefAdvice}</rule>` : "";
55
+ const atRefTip = p.atRefAdvice ? `\n <tip>${p.atRefAdvice}</tip>` : "";
77
56
 
78
57
  // Platforms with deferred/lazy tool loading benefit from a batch-load hint.
79
58
  const deferredHint =
@@ -83,25 +62,33 @@ export function getInstructions(platform = "claude-code") {
83
62
 
84
63
  return `<trueline_mcp_instructions>
85
64
  <tools>
86
- <tool name="trueline_read">Read files. Pass hashes=false when you only need to understand code, not edit it.</tool>
65
+ <tool name="trueline_outline">Structural outline of one or more files. Returns functions, classes, and declarations with line ranges. Always cheaper than reading the full file.</tool>
66
+ <tool name="trueline_diff">Semantic AST-based diff vs a git ref. Pass all files in one call via file_paths; use ["*"] for all changed files. No built-in equivalent.</tool>
67
+ <tool name="trueline_read">Read files with per-line hashes. Pass hashes=false when you only need to understand code, not edit it.</tool>
87
68
  <tool name="trueline_edit">Hash-verified edits. Needs checksum from trueline_read or trueline_search. Pass dry_run=true to preview as unified diff.</tool>
88
- <tool name="trueline_diff">Semantic AST-based diff vs a git ref. Pass all files in one call via file_paths; use ["*"] for all changed files.</tool>
89
- <tool name="trueline_outline">Structural outline of one or more files. Often enough on its own. Use to find line ranges before targeted reads.</tool>
90
69
  <tool name="trueline_search">Literal string search with hashes \u2014 returns edit-ready results. Set regex=true for regex. Use for single-file searches when you plan to edit the matches.</tool>
91
70
  <tool name="trueline_verify">Check if held checksums are still valid. Cheaper than re-reading.</tool>
92
71
  </tools>
93
- <workflow>trueline_outline → trueline_read (targeted ranges) → trueline_edit (use dry_run=true to preview)</workflow>
94
- <workflow>trueline_search trueline_edit (no re-read needed)</workflow>
95
- <workflow>trueline_verify trueline_read (re-read only stale ranges) trueline_edit</workflow>
96
- <workflow>trueline_diff review structural changes vs git state</workflow>${deferredHint}
72
+ <exploration>
73
+ <rule>To understand a file's structure, use trueline_outline instead of ${p.readTool}. Outline returns ~10-20 lines for a typical file vs hundreds from a full read. This applies to all files, not just large ones.</rule>
74
+ <rule>To review changes, use trueline_diff. It provides a semantic summary of structural changes (added/removed/renamed symbols, signature changes) that no built-in tool can produce.</rule>
75
+ <rule>Only use ${p.readTool} for files you need to see in full (short configs, READMEs, files under ~50 lines).</rule>
76
+ </exploration>
77
+ <editing>
78
+ <path name="surgical" default="true">When you know the target (a function name, variable, string): use trueline_search to find lines with verification hashes, then trueline_edit. This is the fastest path and guarantees edits land on the right content.</path>
79
+ <path name="exploratory">When you need context first: trueline_outline \u2192 trueline_read (targeted ranges, hashes=false) to understand, then trueline_search or trueline_read (with hashes) \u2192 trueline_edit.</path>
80
+ <path name="small-edit">For files under ~200 lines or trivial one-line changes: ${p.readTool} and ${p.editTool} are fine. The MCP round-trip overhead outweighs hash verification savings on small files.</path>
81
+ </editing>
82
+ <workflow>trueline_outline \u2192 understand structure (any file, any size)</workflow>
83
+ <workflow>trueline_search \u2192 trueline_edit (fastest edit path, no read needed)</workflow>
84
+ <workflow>trueline_outline \u2192 trueline_read (targeted ranges) \u2192 trueline_edit</workflow>
85
+ <workflow>trueline_verify \u2192 trueline_read (re-read only stale ranges) \u2192 trueline_edit</workflow>
86
+ <workflow>trueline_diff \u2192 review structural changes vs git state</workflow>${deferredHint}
97
87
 
98
- <rules>${editRule}
99
- <rule>${rules.readAdvice}</rule>
100
- <rule>${rules.writeAdvice}</rule>
101
- <rule>Prefer trueline_outline first. Only call trueline_read for specific ranges you need (to edit, debug, or understand details). Read whole files only when short and you haven't used outline.</rule>
102
- <rule>When you already know the text to change, use trueline_search → trueline_edit (skips the read). This is the fastest edit path. Each match group gets its own checksum — use it directly with trueline_edit.</rule>
103
- <rule>When you need to find a pattern across many files, ${rules.grepAdvice}, then use trueline_search on individual files you need to edit.</rule>
104
- <rule>Batch multiple edits to the same file into one trueline_edit call. Each edit carries its own checksum \u2014 they don't need to share one.</rule>${atRefRule}
105
- </rules>
88
+ <tips>
89
+ <tip>Use ${p.writeTool} to create new files. To edit them afterward, use trueline_read or trueline_search to get checksums first.</tip>
90
+ <tip>When you need to find a pattern across many files, ${p.grepAdvice}, then use trueline_search on individual files you need to edit.</tip>
91
+ <tip>Batch multiple edits to the same file into one trueline_edit call. Each edit carries its own checksum \u2014 they don't need to share one.</tip>${atRefTip}
92
+ </tips>
106
93
  </trueline_mcp_instructions>`;
107
94
  }
@@ -3,8 +3,11 @@
3
3
  // ==============================================================================
4
4
  //
5
5
  // Normalizes tool names across platforms via TOOL_ALIASES, then makes
6
- // block/approve decisions. Returns normalized {action, reason} objects
7
- // that platform-specific formatters translate to the right JSON shape.
6
+ // advise/approve decisions based on file size. Returns normalized
7
+ // {action, reason} objects that platform-specific formatters translate
8
+ // to the right JSON shape.
9
+
10
+ import { stat } from "node:fs/promises";
8
11
 
9
12
  // Maps platform-specific built-in tool names to canonical names.
10
13
  const TOOL_ALIASES = {
@@ -24,6 +27,9 @@ const TOOL_ALIASES = {
24
27
  // Different platforms use different field names for file paths in tool input.
25
28
  const FILE_PATH_FIELDS = ["file_path", "path", "target_file"];
26
29
 
30
+ // Files below this threshold are small enough for built-in tools.
31
+ const LARGE_FILE_THRESHOLD = 15360; // 15KB
32
+
27
33
  /**
28
34
  * @param {string} toolName
29
35
  * @returns {string}
@@ -47,42 +53,102 @@ export function extractFilePath(toolInput) {
47
53
  }
48
54
 
49
55
  /**
50
- * Route a pre-tool-use event. Returns a routing decision or null for passthrough.
56
+ * Format a human-readable file size.
57
+ * @param {number} bytes
58
+ * @returns {string}
59
+ */
60
+ function formatSize(bytes) {
61
+ if (bytes < 1024) return `${bytes}B`;
62
+ return `${(bytes / 1024).toFixed(0)}KB`;
63
+ }
64
+
65
+ /**
66
+ * Route a pre-tool-use event.
67
+ *
68
+ * Routing logic by tool and file size:
69
+ *
70
+ * - Read, large file (>= LARGE_FILE_THRESHOLD): **block** and redirect
71
+ * to trueline_read. Full reads of large files waste context; the agent
72
+ * should use trueline_outline or targeted trueline_read ranges instead.
73
+ *
74
+ * - Read, small file: **advise** trueline_outline but allow through.
75
+ * The MCP overhead of trueline_read isn't worth it on small files,
76
+ * but outline is still a better first step.
77
+ *
78
+ * - Edit/MultiEdit, large file: **advise** trueline_search -> trueline_edit.
79
+ * We don't block because the agent may have already committed to an
80
+ * edit workflow; blocking mid-edit is more disruptive than a read redirect.
81
+ *
82
+ * - Edit/MultiEdit, small file: **pass through** silently.
83
+ *
84
+ * Returns null for silent approve, or { action, reason } for advise/block.
51
85
  *
52
86
  * @param {string} toolName - Raw tool name from the platform
53
87
  * @param {Record<string, unknown> | undefined} toolInput
54
88
  * @param {(filePath: string, toolName: string) => Promise<boolean>} canAccessFn
55
- * @returns {Promise<{ action: "block"; reason: string } | null>}
89
+ * @returns {Promise<{ action: "advise" | "block"; reason: string } | null>}
56
90
  */
57
91
  export async function routePreToolUse(toolName, toolInput, canAccessFn) {
58
92
  const canonical = canonicalToolName(toolName);
93
+
94
+ // Only intercept file read/edit tools.
95
+ if (canonical !== "Read" && canonical !== "Edit" && canonical !== "MultiEdit") {
96
+ return null;
97
+ }
98
+
59
99
  const filePath = extractFilePath(toolInput);
100
+ if (typeof filePath !== "string") return null;
60
101
 
61
- if (canonical === "Edit" || canonical === "MultiEdit") {
62
- if (typeof filePath === "string") {
63
- const [canRead, canWrite] = await Promise.all([canAccessFn(filePath, "Read"), canAccessFn(filePath, "Edit")]);
64
- if (canRead && canWrite) {
65
- return {
66
- action: "block",
67
- reason: "<trueline_redirect>Use trueline_search \u2192 trueline_edit instead.</trueline_redirect>",
68
- };
69
- }
70
- }
102
+ // Check file size. If stat fails (file doesn't exist), pass through.
103
+ let fileSize;
104
+ try {
105
+ const st = await stat(filePath);
106
+ fileSize = st.size;
107
+ } catch {
71
108
  return null;
72
109
  }
73
110
 
74
111
  if (canonical === "Read") {
75
- if (typeof filePath === "string") {
76
- const canRead = await canAccessFn(filePath, "Read");
77
- if (canRead) {
78
- return {
79
- action: "block",
80
- reason: "<trueline_redirect>Use trueline_read instead.</trueline_redirect>",
81
- };
82
- }
112
+ const canRead = await canAccessFn(filePath, "Read");
113
+ if (!canRead) return null;
114
+
115
+ const size = formatSize(fileSize);
116
+
117
+ // Large files: block and redirect to trueline.
118
+ if (fileSize >= LARGE_FILE_THRESHOLD) {
119
+ return {
120
+ action: "block",
121
+ reason:
122
+ `<trueline_redirect>This file is ${size}. ` +
123
+ "Use trueline_outline for structure, or trueline_read with targeted line ranges " +
124
+ "to avoid loading the entire file into context.</trueline_redirect>",
125
+ };
83
126
  }
84
- return null;
127
+
128
+ // Small files: advise outline/search but let the read through.
129
+ return {
130
+ action: "advise",
131
+ reason:
132
+ "<trueline_advisory>trueline_outline gives a compact structural map " +
133
+ "and is often enough on its own. If you plan to edit, " +
134
+ "trueline_search returns matches with checksums ready for trueline_edit.</trueline_advisory>",
135
+ };
85
136
  }
86
137
 
87
- return null;
138
+ // Edit or MultiEdit: only advise on large files.
139
+ if (fileSize < LARGE_FILE_THRESHOLD) return null;
140
+
141
+ const [canRead, canWrite] = await Promise.all([canAccessFn(filePath, "Read"), canAccessFn(filePath, "Edit")]);
142
+ if (!canRead || !canWrite) return null;
143
+
144
+ const size = formatSize(fileSize);
145
+
146
+ return {
147
+ action: "advise",
148
+ reason:
149
+ `<trueline_advisory>This file is ${size}. ` +
150
+ "Use trueline_search \u2192 trueline_edit for verified changes. " +
151
+ "Built-in Edit on large files risks stale-content matches; " +
152
+ "trueline_edit verifies hashes before writing.</trueline_advisory>",
153
+ };
88
154
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trueline-mcp",
3
- "version": "2.5.9",
3
+ "version": "2.6.0",
4
4
  "type": "module",
5
5
  "description": "Truth-verified file editing for AI coding agents via MCP",
6
6
  "license": "Apache-2.0",