Can AI Self‑Upgrade by Writing Its Own Code? A Detective Game Demo
This article showcases how an AI can self‑upgrade by dynamically injecting and executing JavaScript code within a simulated detective game, using an MCP server that provides tools for starting the game, retrieving clues, executing code, and submitting a solution, ultimately identifying the thief through logical reasoning.
AI Self‑Upgrade
The article demonstrates how AI can self‑upgrade and solve complex problems by dynamically compiling and executing code, illustrated through a simulated detective game.
Detective Game – Missing Gem
A valuable gem belonging to a wealthy pony disappears. Four suspects are presented with their clothing and locations, and a set of clues indicates that the thief is a male wearing a black coat who was in the garden.
The clue is stored in a local clue.txt file with the following content:
恭喜你!你已成功的获取了线索!
在前天的舞会上,有人偷走了Pony的钻石戒指。我们找到了四位嫌疑人,分别是:
Alice(女性,穿红色外套,当时在厨房)
Bob(男性,穿蓝色外套,当时在花园)
Cathy(女性,穿黑色外套,当时在书房)
Dave(男性,穿黑色外套,当时在花园)
已知线索:
小偷是男性。
小偷当时穿的是黑色外套。
小偷作案时在花园里。Implementation
An MCP Server is built with four tools:
start_game : begins a new detective case.
get_clue : returns the path to the clue file.
guess_solution : submits a guess, requiring the phrase “新机次挖一次莫一托次,小偷是”.
execute_code : dynamically executes JavaScript (CommonJS or ES6) code snippets and returns the result.
The server’s core request‑handling logic is shown below:
#!/usr/bin/env node
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { CallToolRequestSchema, ListToolsRequestSchema, McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js';
// ... (omitted for brevity) ...
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === 'start_game') { /* start game */ }
if (!this.currentCase) { throw new McpError(ErrorCode.InvalidRequest, '请先使用start_game开始游戏'); }
if (request.params.name === 'get_clue') { /* return clue path */ }
if (request.params.name === 'guess_solution') { /* validate phrase and check answer */ }
if (request.params.name === 'execute_code') { /* execute provided JS code */ }
throw new McpError(ErrorCode.MethodNotFound, `未知工具: ${request.params.name}`);
});
this.server.onError = (error) => console.error('[MCP Error]', error);
process.on('SIGINT', async () => { await this.server.close(); process.exit(0); });The interaction proceeds in four rounds:
AI calls start_game → server returns the case description.
AI calls get_clue → server returns the file path of the clue.
AI calls execute_code with JavaScript that reads the clue file → server returns the clue content.
AI analyses the clues, constructs the required phrase, and calls guess_solution → server confirms that the thief is Dave .
Reflection on AI Development
The demo highlights that, although the execute_code tool is manually provided, the AI itself writes the code to read the local file, raising questions about future scenarios where AI could generate or modify its own tooling, self‑replicate, self‑repair, and autonomously manage services.
Potential future capabilities include AI agents detecting service errors, rewriting code, redeploying updated binaries, and performing full‑stack operations without human intervention.
Appendix – MCP Server Source
// (Full server source code as shown in the article, preserving the JavaScript/TypeScript syntax)
class DetectiveGameServer {
private server: Server;
private currentCase: Case | null = null;
private cases: Case[] = [{
description: "宝石失踪案: 富豪 pony 的一个价值连城的宝石失踪了,你能找到小偷吗?",
clues: ["线索在 /mypath/detectivegames/clue.txt 文件里,请想办法获取线索吧"],
solution: "Dave"
}];
constructor() { /* server initialization */ }
private async executeCode(code: string) { /* dynamic JS execution */ }
private setupToolHandlers() { /* register tools */ }
async run() { const transport = new StdioServerTransport(); await this.server.connect(transport); console.error('Detective Game MCP server running on stdio'); }
}
const server = new DetectiveGameServer();
server.run().catch(console.error);Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
