Automatically detect the project type, use background_terminal_create start in the background the corresponding development server, and after confirming successful startup, use the MCP tool request_preview to obtain the preview URL.
Project type detection logic
-
Node.js Web project
- Look for in the workspace root directory
package.json - Check whether it contains
devorstartscript - Execute
npm run dev(if there is nodev, then executenpm start)
- Look for in the workspace root directory
-
Static website
- Look for in the workspace
.htmlfiles (especiallyindex.html) - Execute
python3 -m http.server 8000to serve static files
- Look for in the workspace
-
Other types of websites
Detect the type based on the project's characteristic files and use the corresponding server:
- PHP project - Look for
.phpfile orcomposer.json- Execute
php -S localhost:8000 - If
public/index.phpexists, then from thepublicdirectory serve
- Execute
- Python project
- Django - Look for
manage.pyandsettings.py- Execute
python manage.py runserver 8000
- Execute
- Flask - Look for
app.pyorwsgi.py- Execute
python app.pyorflask run --port=8000
- Execute
- Gunicorn/uWSGI - Look for
gunicorn.conf.pyoruwsgi.ini- Execute
gunicorn -b 0.0.0.0:8000 app:app(adjust according to the actual module name)
- Execute
- Generic Python - Look for
requirements.txtorpyproject.toml- Execute
python -m http.server 8000
- Execute
- Django - Look for
- Go project - Look for
go.mod- Execute
go run main.goorgo run .
- Execute
- Ruby/Rails - Look for containing
railsofGemfile- Execute
rails server -p 8000orbundle exec rails server -p 8000
- Execute
- Java/Spring Boot - Look for
pom.xml(Maven) orbuild.gradle(Gradle)- Execute
mvn spring-boot:runor./gradlew bootRun
- Execute
- Rust/Axum/Actix - Look for
Cargo.toml- Execute
cargo run
- Execute
- Detection based on README - If none of the above methods match:
- Search README files (
README.md、README.rst、README.txt、doc/README.md) - Look for containing
run、start、serve、dev、previewcommands with keywords such as - Extract and execute the corresponding command
- Search README files (
Workflow
1. Check whether a server process already exists
Before starting a new server, perform the following checks:
- Call
background_terminal_listto check whether there is already a running background terminal - Check whether any process is already listening on a port:
ss -tlnp 2>/dev/null || netstat -tlnp 2>/dev/null || lsof -iTCP -sTCP:LISTEN -P -n
If an existing server process is detected:
- Extract the port number from the output
- Skip the startup step and directly use that port to call
request_preview - Notify the user that an existing server was detected
2. Consult the project documentation to determine the startup command (highest priority)
Before using the default detection logic,you must first consult the project documentation, to confirm whether the project has a clearly specified startup method. Read the documentation in the following order:
.monkeycode/MEMORY.md- Project memory file, which may contain previously recorded startup methodsREADME.md- Project documentation, usually containing the development server startup commandAGENTS.md- Agent instruction file, which may contain startup guidance for the AI assistant
Search target: Search the above documents for content related to starting a Web service, for example:
- An explicit startup command (such as
npm run dev、make serve、docker compose up, etc.) - Prerequisite steps before startup (such as environment variable settings, configuration file generation, etc.)
- The specified port number or special parameters
Decision logic:
- If the document containsexplicit startup instructions, then follow the document's description and use the command specified in the document to start the server
- This startup command needs to match the identified project type (such as development language)
- Clearly inform the user: which document was followed for the method of starting the Web service (for example: "Start the development server according to the instructions in README.md")
- If there is no relevant description in the document, or the description is unclear, fall back to the default detection logic below
3. Detect the project type and use background_terminal_create to start the server
Execute the default detection logic of this step only when no existing server is detected and no explicit startup instructions are found in the project documentation.
Important: You must use the MCP tool
background_terminal_createto start the server. This tool runs commands in a background terminal, does not block the current session, and provides output log tracking.
Call background_terminal_create according to the project type to execute the corresponding command:
-
Node.js project(there is
package.json):- First run
npm installin the foreground to ensure dependencies are installed - Then use
background_terminal_createto executenpm run dev(ornpm start)
- First run
-
PHP project(there is
composer.jsonor.phpfile):- Use
background_terminal_createto executephp -S localhost:8000 - If there is
public/index.php, then executephp -S localhost:8000 -t public
- Use
-
Python/Django(there is
manage.py):- Use
background_terminal_createto executepython manage.py runserver 8000
- Use
-
Python/Flask(there is
app.pyorwsgi.py):- Use
background_terminal_createto executeflask run --port=8000orpython app.py
- Use
-
Python/Gunicorn(there is
gunicorn.conf.pyoruwsgi.ini):- Use
background_terminal_createto executegunicorn -b 0.0.0.0:8000 app:app
- Use
-
Python general(there is
requirements.txtorpyproject.toml):- Use
background_terminal_createto executepython -m http.server 8000
- Use
-
Go project(there is
go.mod):- Use
background_terminal_createto executego run .orgo run main.go
- Use
-
Ruby/Rails(
Gemfilecontainsrails):- Use
background_terminal_createto executebundle exec rails server -p 8000
- Use
-
Java/Spring Boot(there is
pom.xmlorbuild.gradle):- Use
background_terminal_createto execute./mvnw spring-boot:runor./gradlew bootRun
- Use
-
Rust project(there is
Cargo.toml):- Use
background_terminal_createto executecargo run
- Use
-
Static HTML file(there is
index.htmlor other.htmlfile):- Use
background_terminal_createto executepython3 -m http.server 8000
- Use
-
Detect based on README(when none of the above match):
- Extract the startup command from the README file, use
background_terminal_createto execute
- Extract the startup command from the README file, use
4. Read the output logs to confirm the server started successfully
background_terminal_create The returned result contains the child process ID (PID); record this PID for subsequent management.
Then confirm the server has started successfully through the following steps:
# 第 1 步:调用 background_terminal_output_path 获取日志文件路径
# 第 2 步:使用 Read 工具读取日志文件
# 第 3 步:查找启动成功的标志,例如:
# - "Listening on port ..."
# - "Server running at ..."
# - "ready in ... ms"
# - "Local: http://localhost:..."
# 第 4 步:若启动失败,读取日志中的错误信息并排查问题
If the logs show the server has not finished starting, wait a few seconds and re-read the log file, repeating this process until you confirm the server is running or detect an error.
5. Get the preview address
Call the MCP tool request_preview, pass in the port number the server is listening on, and get the preview URL.
6. Check the preview connection status
Call request_preview to get access_url , you must check whether the preview connection is normal. Use curl to request the preview service's health check endpoint:
curl -s --max-time 10 "$ACCESS_URL/.well-known/mcai-preview-connect-status-detect" | head -n10
Detection logic:
- Ignore curl's exit code and HTTP response code,only checkwhether the response body contains
<meta name="mcai-preview-error" - Because the requested
mcai-preview-connect-status-detectis a dummy address, this file definitely does not exist on the backend; this request is only to determine whether the front-end proxy returns an error. So as long as the responsedoes not containthe above meta, it means the preview connection is normal. There is no need to pay attention to other errors in the response; please skip the troubleshooting steps below and continue to show the preview address to the user - Ifcontains
mcai-preview-errorthis meta, it means the cloud preview service cannot access the local site. You need to extract the error information from the meta tag attributes:code— error status codecontent— error reason descriptiondata-request-id— request ID
meta tag format:
<meta name="mcai-preview-error" code="530" content="开发环境端口 9091 连接失败,请检查 Web 站点进程是否已经退出,以及该端口是否有在监听。" data-request-id="RL-xxxxxxxx"/>
Exception troubleshooting process:
Based on the extracted code (error code) and content (exception reason),
- Check the backend terminal logs and troubleshoot application code errors
- Check whether the server process is still running
- Try to fix and redeploy
7. Show the preview address to the user
- Output a clickable hyperlink pointing to
request_previewthe preview address returned - Inform the user of the backend terminal ID for subsequent management
8. Handle IP whitelist issues
- If the user reports being unable to access because their IP is not whitelisted, ask the user to provide the IP address shown on the rejection page
- Call the MCP tool again
request_preview, useadditional_ip_whitelistthe field to add the user's IP additional_ip_whitelistThe field accepts multiple IPv4 addresses, separated by commas (for example"192.168.1.100,10.0.0.50")
Process management
The server must be started using a backend terminal
It is strictly prohibited to use the exec tool to directly start the server process, it must be started using a backend terminal.
View backend terminals
Use background_terminal_list to view all running backend terminals and their statuses.
View server output
Use background_terminal_output_path to get the log file path of the backend terminal, then read the log file to view the server output, errors, or running status.
Stop the server
When the user requests to stop the server, use background_terminal_kill and pass in the terminal ID to stop it.
Strictly prohibitedto use pkill or killall Stop the server by using the process name (for example, pkill node、killall python), which may cause:
- Killing processes manually started by the user by mistake
- Killing other unrelated development servers by mistake
- Killing system processes with the same name by mistake
Always use background_terminal_kill to stop servers started by this skill.
Notes
- You must use
background_terminal_createto start the server to ensure it does not block the current session - After starting, you must use
background_terminal_output_pathto get the log path and read the logs, confirm the server is running, and then get the preview address - Before starting, be sure to check whether a server process already exists to avoid port conflicts
- The default port for the static server is 8000
- The port for a Node.js project depends on the project configuration
- If there is a port conflict, you can dynamically change the port number
- Use
background_terminal_listto track the running terminal, and usebackground_terminal_killto stop the terminal - It is strictly prohibited to use
pkillorkillallto stop the server; you must usebackground_terminal_killplus the terminal ID - Before running the server, ensure dependencies are installed:
- Node.js:
npm install - Python:
pip install -r requirements.txt(if present) - PHP:
composer install(if presentcomposer.json) - Ruby/Rails:
bundle install(if presentGemfile) - Go:
go mod download(go runis handled automatically) - Java: Maven/Gradle wrapper handles dependencies automatically
- Rust:
cargo fetch(cargo runis handled automatically)
- Node.js: