Fake LLM Server
Relevant source files
The following files were used as context for generating this wiki page:
- testing/fake-llm-server/index.ts
- testing/fake-llm-server/README.md
- testing/run-fake-stdio-mcp-server.sh
- src/prompts/system_prompt.ts
- src/prompts/security_review_prompt.ts
- e2e-tests/snapshots/local_agent_advanced.spec.ts_local-agent---mcp-tool-call-1.aria.yml
- e2e-tests/snapshots/local_agent_consent.spec.ts_local-agent---add-dependency-consent-decline-1.aria.yml
The Fake LLM Server is a crucial testing utility within the Dyad project. It is an Express.js application designed to simulate the behavior of a real Large Language Model (LLM) for development and end-to-end testing. Its primary purpose is to provide deterministic, predictable, and fast responses for test scenarios, eliminating the latency, cost, and non-determinism associated with making live calls to an actual LLM service.
This server allows developers to create repeatable tests for various AI agent interactions, such as code generation, tool usage, and user consent flows. It works by serving pre-defined response files based on a "test case" identifier found in the user's prompt, making it a cornerstone of the project's automated testing strategy.
Sources: testing/fake-llm-server/README.md, testing/fake-llm-server/index.ts
Architecture and Operation
The server is built with Express.js and can operate in two modes: as a standard HTTP server or communicating over stdin/stdout. The core logic revolves around intercepting requests intended for an LLM, parsing them to identify a test case, and returning a corresponding canned response from the filesystem.
Sources: testing/fake-llm-server/index.ts:13-16, testing/fake-llm-server/README.md
Request Handling Flow
When a request is received, the server extracts the last user message from the request body. It then searches this message for a test case identifier in the format tc=<test-case-name>. If an identifier is found, the server constructs a file path based on this name and attempts to read and serve the content of that file as the LLM's response. If no tc is found, or the file doesn't exist, it returns a default "OK" message.
The following diagram illustrates the request handling process:
This flowchart shows how the Fake LLM Server processes an incoming request to determine which mock response to serve.
Test Case Responses
The server looks for response files in a directory structure that mirrors the test case name. For a test case like tc=local-agent/mcp-calculator, it will search for a file at ../e2e-tests/test-cases/local-agent/mcp-calculator.case.json. These .json files contain the exact response payload the fake server should return, simulating actions like tool calls, file writes, or simple chat messages.
Sources: testing/fake-llm-server/index.ts:70-76, e2e-tests/snapshots/local_agent_advanced.spec.ts_local-agent---mcp-tool-call-1.aria.yml
Simulated Agent Modes
The Fake LLM Server is capable of mimicking different agent "personalities" or modes defined by various system prompts in the main application. The responses it serves are crafted to be consistent with the instructions for each mode.
| Mode | Purpose | Source File |
|---|---|---|
build | The default mode for an AI editor that creates and modifies web applications by writing code, managing files, and adding dependencies. | src/prompts/system_prompt.ts:63-261 |
ask | A helpful assistant mode that provides explanations and guidance on web development concepts without writing any code. | src/prompts/system_prompt.ts:311-404 |
agent | An analysis mode that determines the tools, APIs, and resources needed for a task before the coding phase begins. It does not generate code. | src/prompts/system_prompt.ts:406-455 |
local-agent | A specialized agent that uses a set of tools to interact with the local environment, such as reading/writing files and managing dependencies. | src/prompts/system_prompt.ts:463-465 |
security-review | A security expert persona that analyzes the codebase for vulnerabilities and outputs findings in a structured <dyad-security-finding> format. | src/prompts/security_review_prompt.ts |
Sources: src/prompts/system_prompt.ts, src/prompts/security_review_prompt.ts
Simulated Actions and Responses
The server can simulate a wide range of agent actions by returning responses containing specific XML-like tags that the Dyad application interprets.
Tool Calls
The server can simulate the agent deciding to use an external tool. The snapshot for tc=local-agent/mcp-calculator shows the agent announcing its intent and the UI displaying the tool call and its result.
Sources: e2e-tests/snapshots/local_agent_advanced.spec.ts_local-agent---mcp-tool-call-1.aria.yml
Dependency Management
It can simulate adding a dependency, including the user consent flow. The snapshot for tc=local-agent/add-dependency shows the agent asking for permission to install a package. If the user denies it, the server can simulate the resulting error message.
The sequence diagram below shows the interaction for a denied dependency request.
This diagram illustrates the sequence of events during a test case where the user denies a dependency installation request.
Security Reviews
When simulating a security review, the server returns findings formatted with <dyad-security-finding> tags, as defined in the security review prompt. This allows for testing the UI that displays security vulnerabilities.
Sources: src/prompts/security_review_prompt.ts:15-28
Running the Server
The server can be launched in two ways for different testing contexts.
Standalone HTTP Server
To run it as a regular HTTP server listening on a port, you can use a standard node or ts-node command. This is useful for general development and debugging.
# Example from README
$ ts-node testing/fake-llm-server/index.ts
Sources: testing/fake-llm-server/README.md
Stdio MCP Server
For integration with the Multi-process Communication Protocol (MCP) used in end-to-end tests, the server can be run via a shell script. This script launches the server in a mode where it communicates over stdin and stdout instead of HTTP.
#!/bin/bash
set -euo pipefail
# This script runs the fake stdio mcp server
# It's used in e2e tests to mock the mcp server
cd "$(dirname "$0")/.."
exec pnpm tsx ./fake-llm-server/index.ts --stdio
Content of run-fake-stdio-mcp-server.sh
The --stdio flag instructs the server to use the stdio communication channel.
Sources: testing/run-fake-stdio-mcp-server.sh, testing/fake-llm-server/index.ts:13-26
Conclusion
The Fake LLM Server is an indispensable tool for the development and testing of the Dyad application. By providing a mock backend that can be precisely controlled via test case identifiers, it enables the creation of a robust, comprehensive, and efficient automated testing suite. It ensures that the application's handling of various AI-driven interactions can be verified reliably without external dependencies on live LLM services.