Spaces:
Running
Running
Upload 2 files
Browse files- mcp_server.py +8 -166
- requirements.txt +1 -2
mcp_server.py
CHANGED
@@ -2,7 +2,6 @@ import os
|
|
2 |
import asyncio
|
3 |
from dotenv import load_dotenv
|
4 |
from mcp.server.fastmcp import FastMCP
|
5 |
-
from fastapi.responses import FileResponse, HTMLResponse
|
6 |
from app import Room, rooms, get_ai_move_for_room, get_ai_chat_for_room
|
7 |
|
8 |
load_dotenv()
|
@@ -176,9 +175,12 @@ async def make_move(position: int, room_id: str = None) -> dict:
|
|
176 |
try:
|
177 |
ai_response = get_ai_move_for_room(room)
|
178 |
if ai_response and 'move' in ai_response:
|
179 |
-
|
180 |
-
|
181 |
-
|
|
|
|
|
|
|
182 |
|
183 |
result_message += f"🤖 Mistral AI played O at position {ai_response['move']}\n"
|
184 |
if 'message' in ai_response:
|
@@ -312,39 +314,6 @@ def list_rooms() -> dict:
|
|
312 |
"message": f"Failed to list rooms: {str(e)}"
|
313 |
}
|
314 |
|
315 |
-
@mcp.tool()
|
316 |
-
def switch_room(room_id: str) -> dict:
|
317 |
-
"""
|
318 |
-
Switch to a different active room.
|
319 |
-
Args:
|
320 |
-
room_id (str): Room ID to switch to
|
321 |
-
Returns:
|
322 |
-
dict: Confirmation of room switch with current state
|
323 |
-
"""
|
324 |
-
global current_session
|
325 |
-
try:
|
326 |
-
if room_id not in rooms:
|
327 |
-
return {
|
328 |
-
"status": "error",
|
329 |
-
"message": f"Room {room_id} not found. Use list_rooms() to see available rooms."
|
330 |
-
}
|
331 |
-
|
332 |
-
current_session['active_room_id'] = room_id
|
333 |
-
room = rooms[room_id]
|
334 |
-
|
335 |
-
return {
|
336 |
-
"status": "success",
|
337 |
-
"message": f"Switched to room {room_id}",
|
338 |
-
"room_id": room_id,
|
339 |
-
"markdown_state": room.to_markdown(),
|
340 |
-
"your_turn": room.current_player == 'X' and room.game_status == 'active'
|
341 |
-
}
|
342 |
-
except Exception as e:
|
343 |
-
return {
|
344 |
-
"status": "error",
|
345 |
-
"message": f"Failed to switch room: {str(e)}"
|
346 |
-
}
|
347 |
-
|
348 |
@mcp.tool()
|
349 |
def get_help() -> dict:
|
350 |
"""
|
@@ -379,8 +348,7 @@ def get_help() -> dict:
|
|
379 |
"tips": [
|
380 |
"The AI has personality and will trash talk!",
|
381 |
"You can have multiple rooms active at once",
|
382 |
-
"Use list_rooms() to see all your games"
|
383 |
-
"Use switch_room(room_id) to change between games"
|
384 |
]
|
385 |
},
|
386 |
"available_commands": [
|
@@ -389,136 +357,10 @@ def get_help() -> dict:
|
|
389 |
"send_chat('message') - Chat with AI",
|
390 |
"get_room_state() - Check current game",
|
391 |
"list_rooms() - See all active games",
|
392 |
-
"switch_room(room_id) - Change active room",
|
393 |
"get_help() - Show this help"
|
394 |
]
|
395 |
}
|
396 |
|
397 |
-
@mcp.tool()
|
398 |
-
async def wait_5_seconds() -> dict:
|
399 |
-
"""
|
400 |
-
Wait for 5 seconds. Useful for giving the AI time to respond or timing operations.
|
401 |
-
Returns:
|
402 |
-
dict: Status of the wait operation
|
403 |
-
"""
|
404 |
-
try:
|
405 |
-
await asyncio.sleep(5)
|
406 |
-
return {
|
407 |
-
"status": "success",
|
408 |
-
"message": "Waited 5 seconds successfully"
|
409 |
-
}
|
410 |
-
except Exception as e:
|
411 |
-
return {
|
412 |
-
"status": "error",
|
413 |
-
"message": f"Failed to wait: {str(e)}"
|
414 |
-
}
|
415 |
-
|
416 |
-
# Add web UI endpoints using FastMCP's underlying FastAPI
|
417 |
-
@mcp.get("/")
|
418 |
-
async def root():
|
419 |
-
return {
|
420 |
-
"name": "Tic-Tac-Toe MCP Server",
|
421 |
-
"status": "running",
|
422 |
-
"mcp_tools": ["create_room", "make_move", "send_chat", "get_room_state", "list_rooms"],
|
423 |
-
"web_ui": "/rooms-ui",
|
424 |
-
"description": "MCP server for LeChat + optional web UI for testing"
|
425 |
-
}
|
426 |
-
|
427 |
-
@mcp.get("/rooms-ui")
|
428 |
-
async def serve_ui():
|
429 |
-
return FileResponse("room_game.html")
|
430 |
-
|
431 |
-
@mcp.get("/room_game.js")
|
432 |
-
async def serve_js():
|
433 |
-
return FileResponse("room_game.js")
|
434 |
-
|
435 |
-
# Also serve Flask app routes for the room system
|
436 |
-
from app import app as flask_app
|
437 |
-
from fastapi import Request
|
438 |
-
import json
|
439 |
-
|
440 |
-
@mcp.post("/rooms")
|
441 |
-
async def create_room_endpoint():
|
442 |
-
room = Room()
|
443 |
-
rooms[room.id] = room
|
444 |
-
return {"room_id": room.id, "status": "created", "room_data": room.to_dict()}
|
445 |
-
|
446 |
-
@mcp.get("/rooms/{room_id}")
|
447 |
-
async def get_room_endpoint(room_id: str):
|
448 |
-
if room_id not in rooms:
|
449 |
-
return {"error": "Room not found"}
|
450 |
-
room = rooms[room_id]
|
451 |
-
return {"room_id": room_id, "room_data": room.to_dict(), "markdown": room.to_markdown()}
|
452 |
-
|
453 |
-
@mcp.post("/rooms/{room_id}/move")
|
454 |
-
async def move_endpoint(room_id: str, request: Request):
|
455 |
-
if room_id not in rooms:
|
456 |
-
return {"error": "Room not found"}
|
457 |
-
|
458 |
-
data = await request.json()
|
459 |
-
room = rooms[room_id]
|
460 |
-
position = data.get("position")
|
461 |
-
|
462 |
-
if position is None or position < 0 or position > 8:
|
463 |
-
return {"error": "Invalid position"}
|
464 |
-
|
465 |
-
# Make human move
|
466 |
-
if not room.make_move(position, 'X'):
|
467 |
-
return {"error": "Invalid move"}
|
468 |
-
|
469 |
-
# Check if game ended
|
470 |
-
if room.game_status != 'active':
|
471 |
-
return {"room_data": room.to_dict(), "markdown": room.to_markdown(), "ai_move": None}
|
472 |
-
|
473 |
-
# Get AI move with fallback
|
474 |
-
try:
|
475 |
-
ai_response = get_ai_move_for_room(room)
|
476 |
-
if ai_response and 'move' in ai_response:
|
477 |
-
ai_move = ai_response['move']
|
478 |
-
if 0 <= ai_move <= 8 and room.board[ai_move] == '':
|
479 |
-
room.make_move(ai_move, 'O')
|
480 |
-
if 'message' in ai_response:
|
481 |
-
room.add_chat_message(ai_response['message'], 'ai')
|
482 |
-
else:
|
483 |
-
# Fallback move
|
484 |
-
empty_positions = [i for i in range(9) if room.board[i] == '']
|
485 |
-
if empty_positions:
|
486 |
-
fallback_move = empty_positions[0]
|
487 |
-
room.make_move(fallback_move, 'O')
|
488 |
-
room.add_chat_message("Technical hiccup, but still playing! 🤖", 'ai')
|
489 |
-
|
490 |
-
return {"room_data": room.to_dict(), "markdown": room.to_markdown(), "ai_move": ai_response}
|
491 |
-
except Exception as e:
|
492 |
-
# Fallback on error
|
493 |
-
empty_positions = [i for i in range(9) if room.board[i] == '']
|
494 |
-
if empty_positions:
|
495 |
-
fallback_move = empty_positions[0]
|
496 |
-
room.make_move(fallback_move, 'O')
|
497 |
-
room.add_chat_message("Connection issues, but improvising! 😅", 'ai')
|
498 |
-
|
499 |
-
return {"room_data": room.to_dict(), "markdown": room.to_markdown(), "ai_move": {"move": fallback_move if empty_positions else None}}
|
500 |
-
|
501 |
-
@mcp.post("/rooms/{room_id}/chat")
|
502 |
-
async def chat_endpoint(room_id: str, request: Request):
|
503 |
-
if room_id not in rooms:
|
504 |
-
return {"error": "Room not found"}
|
505 |
-
|
506 |
-
data = await request.json()
|
507 |
-
room = rooms[room_id]
|
508 |
-
user_message = data.get("message", "")
|
509 |
-
|
510 |
-
if not user_message.strip():
|
511 |
-
return {"error": "Empty message"}
|
512 |
-
|
513 |
-
room.add_chat_message(user_message, 'user')
|
514 |
-
|
515 |
-
try:
|
516 |
-
ai_response = get_ai_chat_for_room(room, user_message)
|
517 |
-
room.add_chat_message(ai_response, 'ai')
|
518 |
-
return {"room_data": room.to_dict(), "markdown": room.to_markdown(), "ai_response": ai_response}
|
519 |
-
except Exception as e:
|
520 |
-
return {"error": "AI chat failed"}
|
521 |
-
|
522 |
# --- Server Execution ---
|
523 |
if __name__ == "__main__":
|
524 |
print(f"Tic-Tac-Toe Rooms MCP Server starting on port 7860...")
|
@@ -527,7 +369,7 @@ if __name__ == "__main__":
|
|
527 |
print("- Play against Mistral AI with personality")
|
528 |
print("- Real-time chat with the AI")
|
529 |
print("- Markdown state representation")
|
530 |
-
print("- Room management
|
531 |
print()
|
532 |
print("MCP Tools available:")
|
533 |
print("- create_room()")
|
|
|
2 |
import asyncio
|
3 |
from dotenv import load_dotenv
|
4 |
from mcp.server.fastmcp import FastMCP
|
|
|
5 |
from app import Room, rooms, get_ai_move_for_room, get_ai_chat_for_room
|
6 |
|
7 |
load_dotenv()
|
|
|
175 |
try:
|
176 |
ai_response = get_ai_move_for_room(room)
|
177 |
if ai_response and 'move' in ai_response:
|
178 |
+
# Validate AI move
|
179 |
+
ai_move = ai_response['move']
|
180 |
+
if 0 <= ai_move <= 8 and room.board[ai_move] == '':
|
181 |
+
room.make_move(ai_move, 'O')
|
182 |
+
if 'message' in ai_response:
|
183 |
+
room.add_chat_message(ai_response['message'], 'ai')
|
184 |
|
185 |
result_message += f"🤖 Mistral AI played O at position {ai_response['move']}\n"
|
186 |
if 'message' in ai_response:
|
|
|
314 |
"message": f"Failed to list rooms: {str(e)}"
|
315 |
}
|
316 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
317 |
@mcp.tool()
|
318 |
def get_help() -> dict:
|
319 |
"""
|
|
|
348 |
"tips": [
|
349 |
"The AI has personality and will trash talk!",
|
350 |
"You can have multiple rooms active at once",
|
351 |
+
"Use list_rooms() to see all your games"
|
|
|
352 |
]
|
353 |
},
|
354 |
"available_commands": [
|
|
|
357 |
"send_chat('message') - Chat with AI",
|
358 |
"get_room_state() - Check current game",
|
359 |
"list_rooms() - See all active games",
|
|
|
360 |
"get_help() - Show this help"
|
361 |
]
|
362 |
}
|
363 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
364 |
# --- Server Execution ---
|
365 |
if __name__ == "__main__":
|
366 |
print(f"Tic-Tac-Toe Rooms MCP Server starting on port 7860...")
|
|
|
369 |
print("- Play against Mistral AI with personality")
|
370 |
print("- Real-time chat with the AI")
|
371 |
print("- Markdown state representation")
|
372 |
+
print("- Room management")
|
373 |
print()
|
374 |
print("MCP Tools available:")
|
375 |
print("- create_room()")
|
requirements.txt
CHANGED
@@ -2,5 +2,4 @@ Flask==3.0.0
|
|
2 |
flask-cors==4.0.0
|
3 |
mistralai
|
4 |
python-dotenv
|
5 |
-
mcp
|
6 |
-
fastapi
|
|
|
2 |
flask-cors==4.0.0
|
3 |
mistralai
|
4 |
python-dotenv
|
5 |
+
mcp
|
|