Build Scripts with AI & MCP
Step-by-step walkthrough to connect Cursor, Claude Code, Windsurf, VS Code, or Codex to Sharp Empire's built-in MCP server and build, test, and debug SCAR scripts automatically.
Sharp Empire embeds a full Model Context Protocol (MCP 2025-03-26) server directly inside the game runtime. Instead of copying code by hand, you can describe what you want in plain language and let your AI assistant inspect the live match, write .scar scripts, load/reload them, draw overlays, read runtime errors, and iterate until everything works.
You describe a script idea in plain English
-> AI calls get_scripting_guide & get_match_context
-> AI writes a .scar script via save_script (autoLoad: true)
-> Sharp Empire runs it inside Age of Empires IV
-> AI checks list_running_scripts, get_console_log & draw_list_layers
-> AI fixes any issues and reloads cleanly1. Prerequisites
- Age of Empires IV running with Sharp Empire injected.
- Verify the MCP badge on the SCAR / Lua tab shows MCP Ready (both in-game ImGui and External WebUI).
- Your AI client running locally on the same Windows PC (the MCP server binds strictly to loopback
127.0.0.1for security). - (Optional, only for clients without Streamable HTTP) Node.js 18+ to run the zero-dependency stdio bridge.
2. Connect your AI client
Sharp Empire listens on http://127.0.0.1:38475/mcp (automatically probing 38475–38482 if port 38475 is occupied). You can grab the exact live JSON config with one click by pressing Copy MCP JSON on the SCAR / Lua tab.
Option A — Streamable HTTP (Recommended)
Works natively in Cursor, Claude Code, Windsurf, VS Code (Copilot MCP), Codex, and OpenCode.
Cursor (.cursor/mcp.json or global ~/.cursor/mcp.json)
{
"mcpServers": {
"sharp-empire": {
"url": "http://127.0.0.1:38475/mcp"
}
}
}Claude Code (CLI)
claude mcp add --transport http sharp-empire http://127.0.0.1:38475/mcpCodex (~/.codex/config.toml)
[mcp_servers.sharp-empire]
url = "http://127.0.0.1:38475/mcp"OpenCode (opencode.json)
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"sharp-empire": {
"type": "remote",
"url": "http://127.0.0.1:38475/mcp"
}
}
}Option B — Stdio Bridge (Claude Desktop or stdio-only clients)
If your MCP client only supports local command + args servers, point it at the included zero-dependency bridge script (tools/mcp-bridge/sharp-empire-mcp.mjs). It automatically probes ports 38475–38482 to find the running Sharp Empire host:
{
"mcpServers": {
"sharp-empire": {
"command": "node",
"args": ["<path-to-sharp-empire>/tools/mcp-bridge/sharp-empire-mcp.mjs"]
}
}
}You can override the target URL with the SHARP_EMPIRE_MCP_URL environment variable if needed.
3. Verify the connection
Once connected, open a chat in your AI client and send:
Call get_status on the sharp-empire MCP server and tell me whether I am in a match and how many scripts are loaded.You should see a structured response containing inGame, scriptEngineMode, luaStateReady, scriptsDirectory, webUiUrl, and mcpUrl. The calls badge on Sharp Empire's SCAR / Lua tab increments with every tool call.
4. Walkthrough: Build your first AI-authored script
Enter a Skirmish match (so the SCAR VM is active), then send this prompt to your AI assistant:
Use the sharp-empire MCP server to:
1. Read get_scripting_guide and inspect get_match_context.
2. Create a managed SCAR script named "ai_hello_hud" using save_script with autoLoad: true.
3. Have it update every 0.5s and draw a HUD panel on both ImGui and WebUI showing my civilization, Food, Wood, Gold, Stone, and current population, plus a cyan 3D circle around my starting position.
4. Wait 2 seconds, then call list_running_scripts, get_console_log, and draw_list_layers to verify there are zero errors and the primitives are actively drawing.What the AI does behind the scenes
- Calls
get_scripting_guideto confirmEmpire.ScriptandEmpire.Drawsignatures. - Calls
get_match_contextto verify you are in-game and inspect your live player roster and map dimensions. - Calls
save_script({ name: "ai_hello_hud", code: "...", autoLoad: true })to writeDocuments/empire/ai_hello_hud.scarand reload it cleanly. - Calls
list_running_scriptsto verifystate: "running"andticks > 0. - Calls
get_console_logto confirm yourself:Log(...)fired and zero compile/runtime errors occurred. - Calls
draw_list_layersto confirmprimitiveCount > 0on layer"ai_hello_hud".
5. Built-in MCP prompts & resources
Sharp Empire exposes built-in Resources and Prompts your AI can read directly:
- Resources:
empire://docs/scar-guide— Managed script lifecycle & SCAR essentialsempire://docs/drawing-api— FullEmpire.Draw/EmpireDrawreferenceempire://state/match— Live match telemetry snapshotempire://scripts/list— All saved.scarscripts and their runtime states
- Prompts:
create_scar_script— Guided template prompt for authoring a new managed scriptdebug_scar_script— Guided workflow to inspect logs, fix errors, and reload a failing script
6. Good habits for AI-assisted script development
- Always ask the AI to verify logs after every load: Tell the AI to call
list_running_scriptsandget_console_logaftersave_scriptorreload_script. - Keep SCAR scripts read-only in online matches: Tell the AI "Keep this script strictly read-only + Empire.Draw visuals so it is 100% MP-safe."
- Use
draw_submitfor quick visual experiments: Your AI can test HUD panel layouts or 3D world rings directly withdraw_submitbefore putting them into a.scarfile, then clear them withdraw_clear. - Use
set_featureto coordinate overlays: Your AI can also toggle Sharp Empire options (likemaphack.enable,cards.enable,ranges.enable,zoom.max) directly viaset_feature. See Option Keys Reference.
Troubleshooting
| Symptom | Cause & Fix |
|---|---|
Connection refused on 127.0.0.1:38475/mcp | Make sure Age of Empires IV is running with Sharp Empire injected. Check the live URL in the SCAR / Lua tab in case Sharp Empire probed port 38476+. |
SCAR Lua state is only available inside an active match | You are in the main menu or lobby. Either enter a match (Skirmish/Custom/Multiplayer) or pass engine: "lua" to run_lua for GLUA commands. |
Timed out waiting for game render thread | The game window may be frozen on a loading screen or minimized in an exclusive fullscreen mode. Switch to Borderless Windowed and retry once loaded. |
Script shows state: "error" | Ask your AI to run get_console_log and read_script, fix the Lua error, and run save_script with autoLoad: true. |