beetlebug▾
/

MCP tools

ztcl debug --mcp serves five tools on stdin and stdout, one JSON-RPC message on each line. The runner options after --mcp set the capabilities and the limits of each run. An agent with a JSON file of MCP servers gets the tools with this entry:

{"mcpServers": {"ztcl": {"command": "ztcl", "args": ["debug", "--mcp"]}}}
Tool What it does
ztcl_run Runs a script (path or script) with probes, and gives the report and the output of puts.
ztcl_explain As ztcl_run, with the second run of --explain.
ztcl_records Gives the records of the last run as JSON lines, with where, probe, since, and last.
ztcl_eval Evaluates Tcl code (code) in the interpreter of the last run, for example to read a variable or to add a probe.
ztcl_disasm Gives the bytecode of a proc of the last run (proc), or of a script (script), under the source line of each command, with the state of the JIT. The format is that of ztcl::disasm.

Each tool has the argument budget, the most bytes of its answer (default 4,000). The answer starts with the summary, so an agent reads the status and the error first, and asks for more with detail, last, or ztcl_records.

A session

The file examples/debug/average.tcl divides by zero at its second call of average (Probes and ztcl debug). An agent finds the bug with three tools. The requests of JSON-RPC are in examples/debug/mcp-session.jsonl:

Request Method or tool Arguments
1 initialize
2 ztcl_run {"path":"average.tcl"}
3 ztcl_run {"path":"average.tcl","probes":["-proc average -line 6 -capture {x n}"]}
4 ztcl_records {"where":"x < 0"}
5 ztcl_eval {"code":"average {4 -2 8}"}

Request 2 in full is:

{"jsonrpc":"2.0","id":2,"method":"tools/call",
 "params":{"name":"ztcl_run","arguments":{"path":"average.tcl"}}}

In the file, each request is on one line.

ztcl debug --mcp < mcp-session.jsonl in examples/debug gives one answer for each request. The text of the answers:

  1. ztcl_run gives the error and the locals of the failed frame:

    status: error
    error: divide by zero
    code: ARITH DIVZERO {divide by zero}
    at: expr {$total / $n}
    stack, innermost first (2 frames):
      average {3 -3}
        items = 3 -3
        total = 0
        n = 0
        x = -3
      report
    output:
    avg 6
    
  2. The second ztcl_run adds a probe on line 6 of average. The records show n before line 6 at each item:

    records, oldest first:
      #0 p2 proc=average line=6 x=4 n=0
      #1 p2 proc=average line=6 x=6 n=1
      #2 p2 proc=average line=6 x=8 n=2
      #3 p2 proc=average line=6 x=3 n=0
      #4 p2 proc=average line=6 x=-3 n=1
    
  3. ztcl_records with where x < 0 keeps the one record at the negative item. The answer is one line of JSON. Here it has line breaks between its keys:

    {"seq":4,"units":33,"thread":0,"probe":"p2",
     "kind":"line","proc":"average","line":6,
     "values":{"x":"-3","n":"1"}}
    
  4. ztcl_eval calls average {4 -2 8} in the interpreter of the last run. The answer is 10, and the right average is 3. Line 6 sets n to 0 at a negative item, so the count is wrong also when the run has no error.

tests/debug-examples.sh runs this session and checks these answers.