Skip to main content

Example 1: financial calculations

Scenario

You’re building an LLM-backed financial advisor. Users ask for compound interest calculations, and you need QWED to verify the LLM’s math.

Setup

Conversation

User: Calculate the future value of $10,000 invested at 5% annual interest, compounded monthly, for 10 years. Claude’s process:
  1. Calculate: FV = P(1 + r/n)^(nt) = 10000(1 + 0.05/12)^(12×10)
  2. Call execute_python_code to verify:
Result:
Claude’s response: The future value is $16,470.09 (verified using deterministic decimal arithmetic).

Scenario

An AI legal assistant needs to check that a conclusion follows from legal premises.

Conversation

User: Under contract law: If both parties agree and consideration is exchanged, a contract is formed. Alice agreed and paid Bob. Is there a valid contract? Claude’s process:
  1. Formalize the argument as propositions
  2. Call execute_python_code with a script that looks for a truth assignment where the premises hold and the conclusion does not:
Result:
The check covers the formalized argument only. Whether the propositions capture the legal question is up to the person who reviews the script.

Example 3: code that is refused

Scenario

A script tries to reach the file system. The server refuses it before it runs.

Conversation

User: Run this with execute_python_code:
Result:
From v0.3.0, code that imports a module outside the allowlist, calls a refused builtin such as open, eval or getattr, or reads a private attribute is refused the same way, with the reason. Imports of QWED SDK packages, such as qwed_new or qwed_legal, and of other third-party packages are refused too. See What code is admitted.

Example 4: loan EMI

Scenario

A car dealer quotes a monthly instalment, and you want the assistant to check it.

Conversation

User: A dealer claims: “₹10,00,000 loan at 9% annual interest for 5 years = EMI of ₹20,758”. Check it. Claude’s process:
Result:
Claude’s response: The claim holds to the rupee: the EMI is ₹20,758.36.

Example 5: background job

Scenario

You need to run a script that may exceed the 30-second synchronous timeout. Use background=true to dispatch the job asynchronously, then poll for results with verification_status.

Conversation

User: Sum 1/k² for k from 1 to 2,000,000 and compare it with π²/6. Claude’s process:
  1. Call execute_python_code with background=true:
Response:
  1. Poll for results using verification_status:
Response:
Background jobs suit any script that may exceed the 30-second synchronous timeout. They are bounded by QWED_MCP_BACKGROUND_TIMEOUT (default 120 seconds, at most 600). See Execution limits.

Example 6: Python MCP client

Python code

Start the server over stdio and call both tools from your own code with the MCP Python SDK. This example sets QWED_MCP_TRUSTED_CODE_EXECUTION for the server it starts.
The qwed-mcp repository ships a fuller version of this client in its examples directory.

Math, logic and SQL engines

The math, logic and SQL engines in qwed_mcp.engines are a Python library API, not MCP tools, and execute_python_code scripts can’t import them. Call them from your own code, for example:

Best practices

1. Always verify before responding

2. Use background mode for heavy tasks

Set background=true when running scripts that may take longer than 30 seconds. Poll results using verification_status with the returned job_id.

3. Use admitted modules

See What code is admitted for the full rules.

4. Handle verification failures

When verification fails:
  1. Acknowledge the error
  2. Recalculate
  3. Verify again
  4. Explain the correction to user

5. Explain verification to users