Files
echidna/package.yaml
Emilio Lópezandgustavo-grieco e6baf93926 Replace the Server-Sent Events server with an MCP server (#1607)
* feat: add a call-sequence syntax for MCP clients

Something driving a campaign from outside it needs a way to name a
sequence of calls, and the natural one is how the calls would be written
in Solidity, with a hole wherever the fuzzer should choose:

    approve(0x10, ?); transferFrom(?, ?, 100)

Echidna.MCP.Parse reads that into the list of SolCallPrototypes the
preceding commits taught the fuzzer to generate from. Parsing stops at an
argument's shape rather than its type -- an integer becomes a uint256, an
0x-prefixed literal an address -- because an argument left open carries no
type to compare against anyway, so a prototype is resolved against the
ABI by name and arity either way.

It is a module of its own rather than part of the server that will use
it. This is the one piece of the interface with a set of edge cases worth
enumerating, and Tests.MCPParse enumerates them without needing a
campaign to run against.

Three things differ from the version this is adapted from. `foo(1` no
longer parses as a call with no arguments: the closing parenthesis is
checked rather than assumed, which is what that silently relied on. An
empty sequence is rejected instead of accepted as a sequence of no calls.
And splitting on `;` is Data.List.Split's job rather than a hand-rolled
splitter's -- the bracket-aware split of an argument list still needs its
own, since a comma inside `[1,2]` does not separate arguments.

Nothing parses a sequence yet; the MCP server that does arrives next.

Co-authored-by: gustavo-grieco <[email protected]>

* feat: replace the events server with an MCP server

--server PORT has run a Server-Sent Events server: a one-way stream of
campaign events, enough to watch a campaign with but not to ask it
anything. Replace it with a Model Context Protocol server on the same
flag and the same port, so what is on the other end can both read the
campaign and steer it.

Nine tools. Four report: status (corpus size, iterations, coverage,
tests, optimization values, how long since the last coverage, the
functions that found it, and whatever is being sampled), target,
show_coverage and dump_lcov. Five act: inject_fuzz_transactions and
clear_fuzz_priorities aim the fuzzer at an ordering, sample starts and
stops recording what a function does, execute_sequence replays a concrete
sequence without disturbing the campaign, and reload_corpus picks up
whatever was written to the corpus directory since it started.

The five that act do so over the inter-worker bus, which is why this
comes last: the commands they send, the sampling they switch on and the
replay they ask for all landed in the preceding commits with nothing to
send them. AgentId gains ServerId to say where they come from.

Retiring SSE is the point of the commit rather than a side effect of it.
Echidna.Server had no other caller, so it goes, and wai-extra with it.
The "Waiting until all SSE are received..." drain at the end of a
non-interactive run goes too, and with no reader left the MVar the SIGINT
handler filled goes as well. --server and the `server` config key keep
their names and their meaning -- a port to serve the campaign on -- so
there is no point at which the flag does nothing.

On the dependency: mcp-server comes from Hackage at 0.2.0.1, not from the
personal fork this was prototyped against. Upstream has since released
the two fixes that fork existed for (202 with no body for a notification,
405 for a server-stream GET), so there is no reason to carry a fork of a
library in the closure of a security tool. It needs http-types >= 0.12.6,
which nixpkgs and lts-23.24 both predate: only that version re-exports
`hOrigin` from the umbrella Network.HTTP.Types module that mcp-server
imports unqualified, and http-types has to move for the whole package set
rather than for mcp-server alone, since wai and warp exchange its Status
type with it. Everything in the closure builds against 0.12.6.

Some notes on the shape of it:

The server runs mcpApplication under Warp directly instead of
runMcpServerHttpWithConfig, which announces itself with a bare putStrLn.
Going through a ServerLog event instead keeps the line timestamped and
prefixed like every other one, and leaves Warp's settings to us -- which
matters for binding to loopback.

Origin validation is on with an empty allow-list. The campaign is
reachable from a browser on the same machine, and a page must not be able
to drive a fuzzer through DNS rebinding; a client that sends no Origin at
all, which is every CLI agent, is unaffected.

A tool that cannot do what it was asked answers with an MCP error result
rather than with a report whose text begins "Error:", so a model does not
have to read prose to notice. That is what the Either in ToolRun is.

reload_corpus reads the same two directories the campaign read at
startup, through loadInitialCorpus, rather than the corpus directory
itself -- what is directly in there is those directories, and reading one
as a file is an exception. It decides what is new and adds it inside a
single atomicModifyIORef', because workers are adding to the corpus while
it runs and a read followed by a write would drop whatever arrived in
between. Sequences from disk take the weight of the best entry already
there, never zero, which selectFromCorpus would never draw.

The upstream version's Env.sourceCache is not here: DappInfo already
carries the source cache, so env.dapp.sources is the same thing without a
second copy in Env. Its DumpLcov bus command is not here either -- the
tool calls saveLcovSnapshot directly, and a command with no sender is
what the earlier commits have been leaving out. Its markLines change is
not here: dropping the line number and column separator to save an
agent's tokens would have regressed every text and HTML coverage report
Echidna writes, and an agent that cannot cite a line number is worse off
than one reading a few more tokens.

The recently-covered functions are kept with takeStrict, not take. They
are appended to on every coverage event and read only when a client asks,
so a lazy tail would retain every sequence the campaign ever found
coverage with -- the same trap the sampling state was written around.

One limitation worth knowing: execute_sequence sends every call to the
contract under test, so a sequence naming a function of some other
deployed contract goes to the wrong address. The sequence syntax has no
way to spell an address, so there is nothing to fix it with yet.

Co-authored-by: gustavo-grieco <[email protected]>

* fix: coerce concrete MCP arguments to ABI parameter types

Resolve concrete replay calls against the ABI before constructing SolCall values, and coerce fixed arguments in fuzz prototypes. Preserve signed and full-width literals, validate bounds and array lengths, and reject ambiguous replay overloads.

Add parser, coercion, mixed-prototype and Solidity replay regressions. The focused 63-test suite and live MCP probe pass; restoring the original call construction fails all four call-site regression tests.

* test: cover bytes32 literals in mixed MCP calls

Exercise asymmetric 32-byte words through generic mixed-parameter replay and injected prototypes. Check leading-zero padding, the zero word, and oversized literal rejection for G3, whose implementation is covered by the B1 coercion fix.

Validation: targeted HLint and git diff --check pass. Execution testing is deferred to GitHub CI.

* fix: start MCP only after workers are ready

---------

Co-authored-by: gustavo-grieco <[email protected]>
2026-09-29 18:27:59 +02:00

152 lines
2.8 KiB
YAML

name: echidna
author: Trail of Bits <[email protected]>
maintainer: Trail of Bits <[email protected]>
version: 2.3.3
ghc-options:
- -O2
- -Wall
- -fno-warn-orphans
- -feager-blackholing
# https://github.com/haskell/cabal/issues/4739
- -optP-Wno-nonportable-include-path
- -fspecialize-aggressively
- -fexpose-all-unfoldings
- -Wunused-packages
dependencies:
- aeson
- base
- containers
- directory
- hevm
- MonadRandom
- mtl
- text
language: GHC2021
default-extensions:
- DataKinds
- DuplicateRecordFields
- LambdaCase
- MultiWayIf
- NoFieldSelectors
- OverloadedLabels
- OverloadedRecordDot
- OverloadedStrings
library:
source-dirs: lib/
dependencies:
- ansi-terminal
- array
- base16-bytestring
- binary
- brick
- bytestring
- data-bword
- data-dword
- deepseq
- exceptions
- extra
- filepath
- Glob
- hashable
- html-conduit
- html-entities
- http-conduit
- ListLike
- mcp-server
- mustache
- optics
- optics-core
- process
- random
- rosezipper
- semver
- signal
- split
- stm
- strip-ansi-escape
- time
- unliftio
- unliftio-core
- utf8-string
- vector
- vty
- vty-crossplatform
- warp
- wreq
- word-wrap
- xml-conduit
- yaml
executables:
echidna:
main: Main.hs
source-dirs: src/
dependencies:
- ansi-terminal
- code-page
- echidna
- filepath
- hashable
- optparse-applicative
- strip-ansi-escape
- time
- with-utf8
ghc-options:
- -threaded
- -rtsopts
- '"-with-rtsopts=-A64m -N"'
when:
- condition: (os(linux) || os(windows)) && flag(static)
ghc-options:
- -optl-static
- condition: os(linux) || os(windows)
ghc-options:
- -O2
- -optl-pthread
- condition: os(darwin)
ld-options: -Wl,-keep_dwarf_unwind
ghc-options: -fcompact-unwind
tests:
echidna-testsuite:
main: Spec.hs
source-dirs: src/test
dependencies:
- bytestring
- data-dword
- echidna
- exceptions
- optics-core
- process
- semver
- split
- tasty
- tasty-hunit
- tasty-quickcheck
- vector
- yaml
when:
- condition: (os(linux) || os(windows)) && flag(static)
ghc-options:
- -optl-static
- condition: os(linux) || os(windows)
ghc-options:
- -O2
- -optl-pthread
- condition: os(darwin)
ld-options: -Wl,-keep_dwarf_unwind
ghc-options: -fcompact-unwind
flags:
static:
description: Pass -static to ghc when linking the stack binary.
manual: true
default: false