Skip to main content
Glama

solve_z3

Solve constraint satisfaction and optimization problems using the Z3 solver. Processes structured problem definitions with variables and constraints to determine satisfiability and find solutions.

Instructions

Solve a Z3 constraint satisfaction problem.

Takes a structured problem definition and returns a solution using Z3 solver.
Handles both satisfiability and optimization problems.

Args:
    problem: Problem definition containing variables and constraints

Returns:
    Solution results as TextContent list, including values and satisfiability status

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
problemYes

Implementation Reference

  • The primary handler function for the 'solve_z3' MCP tool. It receives a Z3Problem input, calls the underlying solve_problem helper, and returns the result formatted as JSON in TextContent.
    @app.tool("solve_z3")
    async def solve_z3(problem: Z3Problem) -> list[TextContent]:
        """Solve a Z3 constraint satisfaction problem.
    
        Takes a structured problem definition and returns a solution using Z3 solver.
        Handles both satisfiability and optimization problems.
    
        Args:
            problem: Problem definition containing variables and constraints
    
        Returns:
            Solution results as TextContent list, including values and satisfiability status
        """
        result = solve_problem(problem)
    
        match result:
            case Success(solution):
                return [
                    TextContent(
                        type="text",
                        text=json.dumps(
                            {
                                "values": solution.values,
                                "is_satisfiable": solution.is_satisfiable,
                                "status": solution.status,
                            }
                        ),
                    )
                ]
            case Failure(error):
                return [TextContent(type="text", text=f"Error solving problem: {error}")]
            case _:
                return [TextContent(type="text", text="Unexpected error in solve_z3")]
  • FastMCP tool registration decorator binding the solve_z3 handler to the tool name.
    @app.tool("solve_z3")
  • Pydantic BaseModel defining the input schema (Z3Problem) for the solve_z3 tool, including variables and constraints.
    class Z3Problem(BaseModel):
        """Complete Z3 constraint satisfaction problem."""
    
        variables: list[Z3Variable]
        constraints: list[Z3Constraint]
        description: str = ""
  • Core solver logic that creates Z3 variables and constraints from the problem model, solves using z3.Solver.check(), and extracts the solution values.
    def solve_problem(problem: Z3Problem) -> Result[Z3Solution, str]:
        """Orchestrates the complete process of solving a Z3 problem.
    
        Args:
            problem: The problem definition containing variables and constraints.
    
        Returns:
            Result: A Result containing either a Solution object or a failure with error details.
        """
        try:
            vars_result = create_variables(problem.variables)
            if isinstance(vars_result, Failure):
                return vars_result
    
            variables = vars_result.unwrap()
    
            constraints_result = create_constraints(problem.constraints, variables)
            if isinstance(constraints_result, Failure):
                return constraints_result
    
            z3_constraints = constraints_result.unwrap()
    
            solve_result = solve(variables, z3_constraints)
            if isinstance(solve_result, Failure):
                return solve_result
    
            result_tuple = solve_result.unwrap()
    
            return extract_solution(result_tuple[0], result_tuple[1], variables)
        except Exception as e:
            return Failure(f"Error solving problem: {e!s}")
  • Supporting Pydantic models and enum for Z3 variables, constraints, problem, and solution used by the solve_z3 tool.
    class Z3VariableType(str, Enum):
        """Variable types in Z3."""
    
        INTEGER = "integer"
        REAL = "real"
        BOOLEAN = "boolean"
        STRING = "string"
    
    
    class Z3Variable(BaseModel):
        """Typed variable in a Z3 problem."""
    
        name: str
        type: Z3VariableType
    
    
    class Z3Constraint(BaseModel):
        """Constraint in a Z3 problem."""
    
        expression: str  # expression as string (run through eval)
        description: str = ""
    
    
    class Z3Problem(BaseModel):
        """Complete Z3 constraint satisfaction problem."""
    
        variables: list[Z3Variable]
        constraints: list[Z3Constraint]
        description: str = ""
    
    
    class Z3Solution(BaseModel):
        """Solution to a Z3 problem."""
    
        values: dict[str, Z3Value]
        is_satisfiable: bool
        status: str
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden for behavioral disclosure. It mentions the tool 'returns a solution using Z3 solver' and the return format, but lacks critical behavioral details: whether it's deterministic, time/complexity characteristics, error handling, memory usage, or what happens with unsatisfiable problems beyond the mention of 'satisfiability status'.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is reasonably concise with clear sections (purpose, capabilities, Args, Returns). However, the Args section adds little value and could be more efficiently integrated. The structure is front-loaded with the core purpose, but some sentences like 'Takes a structured problem definition' are redundant with the schema.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a complex constraint solving tool with 1 parameter but deeply nested schema (Z3Problem with variables and constraints), no annotations, and no output schema, the description is inadequate. It doesn't explain how to structure problems, what Z3 syntax to use in constraints, or provide examples. The mention of 'TextContent list' return format is vague without schema details.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The description adds minimal value beyond the input schema. It states 'Takes a structured problem definition' and documents the 'problem' parameter, but with 0% schema description coverage, it doesn't explain the structure of Z3Problem, Z3Variable, or Z3Constraint objects. The Args section merely repeats parameter names without adding meaningful semantics.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Solve a Z3 constraint satisfaction problem' with the verb 'solve' and resource 'Z3 constraint satisfaction problem'. It distinguishes from some siblings like 'simple_cvxpy_solver' by specifying Z3, but doesn't explicitly differentiate from 'solve_z3_simple' which could cause confusion.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description mentions 'Handles both satisfiability and optimization problems' which provides some context, but offers no explicit guidance on when to use this tool versus the six listed sibling tools. There's no mention of prerequisites, alternatives, or specific scenarios where Z3 is preferred over other solvers.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sdiehl/usolver'

If you have feedback or need assistance with the MCP directory API, please join our Discord server