Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 237 additions & 0 deletions examples/agents/snake_game_generator.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Snake Game Generator - AI Creates a Game\n",
"\n",
"This notebook shows how to use aisuite + MCP tools to have an AI generate a complete, playable Snake game.\n",
"\n",
"**What it does**: The LLM generates a Snake game in HTML/CSS/JavaScript, saves it to a file using MCP filesystem tools, and we display it right in the notebook.\n",
"\n",
"**Requirements**: `OPENAI_API_KEY` or `ANTHROPIC_API_KEY` in your `.env` file (depending on which model you choose)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✓ Ready!\n"
]
}
],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"import aisuite as ai\n",
"from aisuite.mcp import MCPClient\n",
"from IPython.display import IFrame, display # For displaying HTML file\n",
"\n",
"load_dotenv()\n",
"\n",
"# Initialize filesystem MCP server for file writing\n",
"filesystem_mcp = MCPClient(\n",
" command=\"npx\",\n",
" args=[\"-y\", \"@modelcontextprotocol/server-filesystem\", os.getcwd()]\n",
")\n",
"\n",
"print(\"✓ Ready!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Craft Instructions"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"prompt = \"\"\"Create a complete, playable Snake game.\n",
"\n",
"**EXECUTION RULES:**\n",
"- Execute ALL tools silently (no intermediate text responses)\n",
"- Write the HTML file FIRST, then provide a brief summary\n",
"\n",
"**GAME REQUIREMENTS:**\n",
" Styling:\n",
" - Clean, modern look\n",
" - Centered on page\n",
" - Nice colors (dark background, bright snake, contrasting food)\n",
" - Clear score display\n",
" - Arrow keys to change direction\n",
" - Instructions shown on screen\n",
"\n",
"**Save the file:**\n",
" - Use write_file to save as 'snake_game.html'\n",
" - After saving, respond with confirmation that the game was created\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run Agent with MCP Tools"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"client = ai.Client()\n",
"tools = filesystem_mcp.get_callable_tools()\n",
"\n",
"# Choose your model (uncomment one):\n",
"model = \"openai:gpt-5.1\"\n",
"# model = \"anthropic:claude-sonnet-4-5\"\n",
"\n",
"response = client.chat.completions.create(\n",
" model=model,\n",
" messages=[{\"role\": \"user\", \"content\": prompt}],\n",
" tools=tools,\n",
" max_turns=5\n",
")\n",
"\n",
"print(\"✓ Done!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Play the Game"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <iframe\n",
" width=\"600\"\n",
" height=\"800\"\n",
" src=\"snake_game.html\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" \n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x11c9dbe00>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"💡 Open 'snake_game.html' in your browser for full view\n"
]
}
],
"source": [
"if os.path.exists('snake_game.html'):\n",
" display(IFrame(src='snake_game.html', width=600, height=800))\n",
" print(\"\\n💡 Open 'snake_game.html' in your browser for full view\")\n",
"else:\n",
" print(\"⚠️ Game not created. Printing response from the model:\")\n",
" print(f\"\\n{response.choices[0].message.content}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f\"\\n{response.choices[0].message.content}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Cleanup"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"filesystem_mcp.close()\n",
"print(\"✓ Done!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## That's It!\n",
"\n",
"In just a few lines of code, you had an AI:\n",
"- ✅ Generate a complete Snake game from scratch\n",
"- ✅ Save it to disk using MCP filesystem tools\n",
"- ✅ Display it playable right in the notebook\n",
"\n",
"**Try it yourself:**\n",
"- Ask for a different game (Pong, Tetris, etc.)\n",
"- Add difficulty levels or speed settings\n",
"- Request different color themes\n",
"- Try with different models (swap the commented line)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading