solve-sudoku
Solve Sudoku puzzles by inputting the puzzle name into the MCP server. Designed to process and provide solutions for Sudoku challenges efficiently.
Instructions
Solve a Sudoku puzzle
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | Name of the puzzle to solve |
Implementation Reference
- src/sodukusolver/server.py:241-282 (handler)Handler for the 'solve-sudoku' tool: retrieves named puzzle from state, solves it in-place using solveSudoku helper, stores solution, notifies of resource change, returns formatted grid or error.elif name == "solve-sudoku": puzzle_name = arguments.get("name") if not puzzle_name: raise ValueError("Missing puzzle name") if puzzle_name not in sudoku_puzzles: return [ types.TextContent( type="text", text=f"Sudoku puzzle '{puzzle_name}' not found", ) ] # Make a copy of the grid to solve grid = [row[:] for row in sudoku_puzzles[puzzle_name]] # Solve the puzzle if solveSudoku(grid): # Store the solved puzzle with a new name solved_name = f"{puzzle_name}_solved" sudoku_puzzles[solved_name] = grid # Notify clients that resources have changed await server.request_context.session.send_resource_list_changed() # Return formatted solution formatted_grid = format_sudoku_grid(grid) return [ types.TextContent( type="text", text=f"Solved Sudoku puzzle '{puzzle_name}':\n\n{formatted_grid}", ) ] else: return [ types.TextContent( type="text", text=f"No solution found for Sudoku puzzle '{puzzle_name}'", ) ]
- src/sodukusolver/server.py:152-162 (registration)Registration of the 'solve-sudoku' tool in the list_tools() method, including its description and input schema requiring a 'name' parameter.types.Tool( name="solve-sudoku", description="Solve a Sudoku puzzle", inputSchema={ "type": "object", "properties": { "name": {"type": "string", "description": "Name of the puzzle to solve"}, }, "required": ["name"], }, ),
- src/sodukusolver/server.py:155-161 (schema)JSON Schema for 'solve-sudoku' tool input: object with required 'name' string property.inputSchema={ "type": "object", "properties": { "name": {"type": "string", "description": "Name of the puzzle to solve"}, }, "required": ["name"], },
- src/sodukusolver/solver.py:70-93 (helper)Core backtracking Sudoku solver function using bitmasks for row/col/box tracking, called by the tool handler to solve the puzzle grid.def solveSudoku(mat): """ Solve a Sudoku puzzle. Args: mat: The Sudoku grid (9x9 matrix) with 0s for empty cells Returns: bool: True if solved successfully, False otherwise """ n = len(mat) row = [0] * n col = [0] * n box = [0] * n # Set the bits in bitmasks for values that are initially present for i in range(n): for j in range(n): if mat[i][j] != 0: row[i] |= (1 << mat[i][j]) col[j] |= (1 << mat[i][j]) box[(i // 3) * 3 + j // 3] |= (1 << mat[i][j]) return sudokuSolverRec(mat, 0, 0, row, col, box)
- src/sodukusolver/solver.py:175-203 (helper)Helper function to format the solved Sudoku grid into a readable bordered text representation, used in the tool response.def format_sudoku_grid(grid): """ Format a Sudoku grid for display. Args: grid: 9x9 Sudoku grid Returns: str: Formatted string representation of the grid """ result = [] horizontal_line = "+-------+-------+-------+" for i in range(9): if i % 3 == 0: result.append(horizontal_line) row = "| " for j in range(9): row += str(grid[i][j]) + " " if (j + 1) % 3 == 0: row += "| " result.append(row) result.append(horizontal_line) return "\n".join(result)