
Prolog Reasoner
io.github.rikarazome由 GitHub 帳號 rikarazome 發布v0.2.1更新於 Oct 24, 2026
SWI-Prolog as a logic calculator for LLMs
概覽
一個本機 MCP 伺服器,執行 SWI-Prolog,讓助理把邏輯問題寫成可執行的 Prolog 程式來求解,而不是靠猜測。
- 可以做什麼
- 執行 Prolog 事實、規則與查詢,回傳結構化結果、錯誤資訊與耗時中繼資料,可選擇附上證明樹並限制結果數量。儲存、列出、讀取與刪除具名規則庫,讓穩定的領域規則能依名稱在多次呼叫間重複使用。在執行呼叫中引用已儲存的規則庫,每次只需寫入與當前情境相關的事實。依類別回傳錯誤並附上自然語言提示,協助助理修正寫錯的 Prolog。
- 適用情境
- 適合助理需要處理限制條件滿足、多步推理、窮舉搜尋等單靠語言推理不可靠的形式邏輯問題,或希望推理過程以可檢查、可重跑的程式碼形式存在時使用。
- 執行需求
- 以本機 stdio 程序執行,通常透過 uvx 從 prolog-reasoner 的 PyPI 套件啟動。需要 Python 3.10 以上,以及 PATH 中的 SWI-Prolog 9.0 以上,或使用已內建 SWI-Prolog 的 Docker 映像。MCP 伺服器本身不需要 API 金鑰;規則庫存放在可設定的規則目錄中。
安裝
在 SourceWeft 中
- 開啟 儀表板中的 Prolog Reasoner,將其新增到工作區。
- 為需要使用其工具的對話啟用該服務。
Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。
其他 MCP 客戶端
參照 儲存庫 中的啟動說明。
README
prolog-reasoner
[PyPI version] [Python versions] [CI] [License: MIT]
SWI-Prolog as a "logic calculator" for LLMs — available as an MCP server and a Python library. Eliminate the black box from LLM logical reasoning.
LLMs excel at natural language but struggle with formal logic. Prolog excels at logical reasoning but can't process natural language. prolog-reasoner bridges this gap by exposing SWI-Prolog execution to LLMs.
Does it help?
On the built-in 30-problem logic benchmark:
The gap concentrates in constraint satisfaction and multi-step reasoning — the combinatorial territory LLMs are weak on and Prolog is strong on. Full breakdown below.
Why it works
LLMs pattern-match; Prolog actually searches and solves. When the LLM writes its problem down as Prolog, two things happen at once:
- Prolog handles the combinatorial work LLMs are weak on — constraint satisfaction, multi-step inference, exhaustive search.
- The reasoning exists as code you can read, re-run, and debug. When it goes wrong, you see the exact Prolog that failed and why.
Two ways to use it
- MCP server — Claude (or any MCP client) calls it as a logic solver during conversation. Rule bases let the LLM save stable domain rules once and reference them by name per call.
- Python library — full NL→Prolog pipeline with self-correction. Requires OpenAI or Anthropic.
Features
- MCP tools:
execute_prologfor arbitrary SWI-Prolog execution, pluslist_rule_bases/get_rule_base/save_rule_base/delete_rule_basefor reusable named rule bases (v14) - Rule bases: save stable Prolog rules once (e.g. chess move rules, legal axioms) and reference them by name from
execute_prologso the LLM only writes the situation-specific facts per call - Transparent intermediate representation: the Prolog code is the audit trail — inspect, modify, or verify before execution
- CLP(FD) support: constraint logic programming for scheduling and optimization
- Negation-as-failure, recursion, all standard SWI-Prolog features
- Library mode: NL→Prolog translation with self-correction loop (OpenAI / Anthropic)
Requirements
- Python ≥ 3.10
- SWI-Prolog installed and on PATH (≥ 9.0)
- API key for OpenAI or Anthropic — only for library mode, not for the MCP server
Installation
MCP Server Setup
The MCP server exposes five tools — execute_prolog runs Prolog code written by the connected LLM, and four rule-base tools manage named, reusable Prolog modules. It does not call any external LLM API, so no API key is required.
Claude Desktop / Claude Code
Or, if prolog-reasoner is installed directly:
Docker (SWI-Prolog bundled)
Use Docker if you don't want to install SWI-Prolog locally:
Tool reference
execute_prolog(prolog_code, query, rule_bases=None, max_results=100, trace=False)
prolog_code— Prolog facts and rules (string)query— Prolog query to run, e.g."mortal(X)"(string)rule_bases— optional list of saved rule base names to prepend toprolog_code(in order). Use this to reuse stable domain rules across calls without re-sending themmax_results— cap the number of solutions returned (default 100)trace— whenTrue, attach a structured proof tree per solution tometadata.proof_trace. Opt-in sub-feature; has performance overhead and does not support CLP(FD), higher-order predicates, or assert/retract.
Returns a JSON object with success, output, query, error, and metadata.
On success, metadata includes execution_time_ms, result_count, truncated, and rule_bases_used. When rule bases were requested, rule_base_load_ms is also attached (disk I/O timing). On failure, metadata also includes error_category (one of syntax_error, undefined_predicate, unbound_variable, type_error, domain_error, evaluation_error, permission_error, timeout, trace_mechanism_error, unknown) and error_explanation — a natural-language hint for the connected LLM (or human) to decide how to fix the Prolog code.
Rule base tools — manage named, reusable Prolog modules under PROLOG_REASONER_RULES_DIR (defaults to ~/.prolog-reasoner/rules/). Names are restricted to [a-z0-9_-], length 1–64.
save_rule_base(name, content)— write or overwrite a rule base. Content is syntax-validated (parse-only) before the write; failures surface asRULEBASE_003. Returns{"success": true, "name": ..., "created": bool}wherecreatedistrueon first write,falseon overwrite. Files overmax_rule_sizeare rejected withRULEBASE_005.list_rule_bases()— return all saved rule bases withname,description, andtags. Metadata is extracted from leading% description:/% tags:comments in each file.get_rule_base(name)— return the raw Prolog source of a saved rule base.delete_rule_base(name)— remove a saved rule base.
For name/size/existence errors, the tools return {"success": false, "error": "...", "error_code": "RULEBASE_001"|"RULEBASE_002"|"RULEBASE_003"|"RULEBASE_005"} rather than raising. I/O failures (RULEBASE_004) are propagated as infrastructure errors.
Rule base conventions — start each rule base file with leading comments that double as list_rule_bases metadata:
Then reference from execute_prolog:
Rule bases also serve as the foundation for domain-specialized forks: ship a curated set (legal axioms, game rules, tax scenarios, etc.) bundled via BUNDLED_RULES_DIR as a ready-to-use reasoning package.
Library Usage
The library exposes PrologExecutor (Prolog-only, no LLM) and PrologReasoner (NL→Prolog pipeline, needs an LLM API key).
Execute Prolog directly (no LLM)
Full NL→Prolog pipeline (requires LLM API key)
Configuration
All settings via environment variables (prefix PROLOG_REASONER_):
Benchmark
benchmarks/ contains 30 logic problems across 5 categories (deduction, transitive, constraint, contradiction, multi-step) to compare LLM-only reasoning vs LLM+Prolog reasoning. The benchmark exercises the library path (translator + executor), since it requires the NL→Prolog step.
Results
Measured on anthropic/claude-sonnet-4-6, single run over 30 problems:
Per-category breakdown:
The gap is concentrated in constraint (SEND+MORE, 6-queens, knapsack, K4 coloring, Einstein-lite) and multi-step (Nim game theory, 3-person knights-and-knaves, TSP-4, zebra puzzle) — exactly the combinatorial/search-heavy territory where symbolic solvers outperform pattern completion. On purely deductive or transitive questions the LLM is already strong and Prolog adds latency without accuracy gains.
All 3 LLM+Prolog failures were Prolog execution errors from malformed LLM-generated code (missing predicate definitions, unbound CLP(FD) variables) rather than reasoning errors — addressable via prompt tuning. Notably, every failure is inspectable: you can see the exact Prolog that failed and why, rather than a wrong natural-language answer with no explanation.
Running it yourself
Results are saved to benchmarks/results.json.
Comparison with other Prolog MCPs
Several Prolog MCP servers exist, each with different design choices. prolog-reasoner is intentionally stateless and spot-use — Prolog is a calculator you call when logic matters, not the backbone of your agent's memory.
This is also why accuracy benchmarks are published here and not elsewhere: statelessness is what makes a side-by-side comparison possible.
If you need persistent agent memory, hallucination-safeguarded fact storage, or a full neuro-symbolic substrate, other projects may fit better:
- adamrybinski/prolog-mcp — Trealla WASM with save/load sessions
- umuro/prolog-mcp — layered KB with file-backed persistence
- vpursuit/model-context-lab — SWI-Prolog with security sandboxing
- dr3d/prolog-reasoning — neuro-symbolic memory with write-path safety
We're the spot-use option.
Development
License
MIT
來源:README.md,提交 3267ca0
工具
0版本歷史
1- v0.2.1最新Sep 16, 2026
評分
已滿足適用檢查項的 50%
該評分衡量此收錄條目的完整程度,依據註冊中心條目、端點與公開來源計算得出。它不是對伺服器本身的評價,也不表示伺服器安全或目前可用:可連通性是帶日期的檢查結果,評分也不包含安裝量或使用者評價。
- 近 30 天內可連通25 分
請保持端點能夠回應 MCP 握手;SourceWeft 每 30 天探測一次。
- 已宣告認證方式10 分
請在註冊中心條目中宣告憑證請求標頭,或保持端點能夠回應探測,以便判定其認證方式。
- 已列出工具15 分
請讓伺服器在無憑證時回應 tools/list,或發布可在沙箱中無憑證啟動的軟體包。
- 描述必需15 分
- 圖示10 分
- 文件15 分
- 授權條款10 分