Logic Server
A constraint-based logic solver with LLM integration for solving logic puzzles and formal reasoning. Supports stateless reasoning, stateful sessions, and MCP tool exposure for Claude Desktop and Claude Code.
Supported clients: Claude Code, Codex CLI, Gemini CLI, Claude Desktop
Features
✅ Session-based reasoning - Accumulate facts and rules across multiple turns ✅ Logic queries - Pattern matching with variables and constraints ✅ Rule derivation - Define logical rules to infer new facts ✅ MCP integration - Works with Claude Code, Codex CLI, Gemini CLI, and Claude Desktop ✅ Performance - Optional 50x speedup with janus-swi
Installation
For Users (Recommended):
# Install from PyPI
pip install prolog-logic-server
# Or install a global CLI with pipx
pipx install prolog-logic-server
# Or with uv (fast, isolated)
uv tool install prolog-logic-server
This installs the logic_server_mcp command for easy integration with Claude.
For Development:
# Clone and install in development mode
cd /path/to/prolog
pip install -e .
# Install development tools
pip install -e ".[dev]"
Requirements:
- Python 3.10+
- SWI-Prolog (
swipl) must be installed and on PATH
Install SWI-Prolog:
# macOS
brew install swi-prolog
# Ubuntu/Debian
apt-get install swi-prolog
# Windows
# Download from https://www.swi-prolog.org/Download.html
Quick Start
Step 1: Install once
pip install prolog-logic-server
This installs the logic_server_mcp command for MCP clients.
Step 2: Pick your client
Claude Code (Recommended for Development)
# Add to Claude Code
claude mcp add --transport stdio logic-reasoning -- logic_server_mcp
# Verify it's connected
claude mcp list
Development mode:
# Test the server first (Ctrl+C to stop)
python -m logic_server.mcp.stateful
# Add to Claude Code (use module invocation)
claude mcp add --transport stdio logic-reasoning -- python3 -m logic_server.mcp.stateful
Usage in Claude Code:
# Start Claude Code
claude
# Then ask:
You: "Create a reasoning session and solve this logic puzzle:
Alice is 30, Bob is 17. Who is an adult (over 18)?"
Claude: [Automatically uses the logic server to solve!]
Codex CLI
# Add to Codex
codex mcp add --transport stdio logic-reasoning -- logic_server_mcp
# Verify it's connected
codex mcp list
Development mode:
# Test the server first (Ctrl+C to stop)
python -m logic_server.mcp.stateful
# Add to Codex (use module invocation)
codex mcp add --transport stdio logic-reasoning -- python3 -m logic_server.mcp.stateful
Usage in Codex:
# Start Codex
codex
# Then ask:
You: "Create a reasoning session and solve this logic puzzle:
Alice is 30, Bob is 17. Who is an adult (over 18)?"
Codex: [Automatically uses the logic server to solve!]
Gemini CLI
# Add to Gemini CLI (user scope writes to ~/.gemini/settings.json)
gemini mcp add -s user -t stdio logic-reasoning logic_server_mcp
# Verify it's connected
gemini mcp list
Development mode:
# Test the server first (Ctrl+C to stop)
python -m logic_server.mcp.stateful
# Add to Gemini CLI (use module invocation)
gemini mcp add -s user -t stdio logic-reasoning python3 -m logic_server.mcp.stateful
Usage in Gemini CLI:
# Start Gemini CLI
gemini
# Then ask:
You: "Create a reasoning session and solve this logic puzzle:
Alice is 30, Bob is 17. Who is an adult (over 18)?"
Gemini: [Automatically uses the logic server to solve!]
Claude Desktop
If you skipped Step 1 above, install the package first:
pip install prolog-logic-server
Configure Claude Desktop
Edit configuration file:
- macOS/Linux:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json
Add this configuration:
{
"mcpServers": {
"logic-reasoning": {
"command": "logic_server_mcp"
}
}
}
Development mode (for contributors):
{
"mcpServers": {
"logic-reasoning": {
"command": "python3",
"args": ["-m", "logic_server.mcp.stateful"]
}
}
}
Restart Claude Desktop
- Quit completely (Cmd+Q on Mac)
- Restart Claude Desktop
- Look for 🔌 icon - should show "logic-reasoning" connected
Other ways to use the server
CLI (Standalone)
Run queries directly from command line:
# Query Prolog fact files
python -m logic_server.cli.main examples/puzzles/simple_pet_facts.pl --query "person(P)."
# With max solutions limit
python -m logic_server.cli.main examples/puzzles/complex_pet_facts.pl --query "pet(P)." --max-solutions 3
Python API
from logic_server.core import create_session, assert_facts, assert_rules, query
# Create session
session_id = create_session()
# Add facts
assert_facts(session_id, [
"person(alice).",
"age(alice, 30).",
"person(bob).",
"age(bob, 17)."
])
# Add rules
assert_rules(session_id, [
"adult(X) :- person(X), age(X, A), A >= 18"
])
# Query
result = query(session_id, query="adult(X)")
print(result) # {"success": true, "solutions": [{"Bindings": {"X": "alice"}}], ...}
What You Can Do
Once integrated with Claude:
✅ Contract analysis - Extract facts from documents, query obligations ✅ Logic puzzles - Solve constraint satisfaction problems ✅ Multi-turn reasoning - Build knowledge graphs across conversation ✅ Formal verification - Every answer backed by logical proof ✅ Complex queries - Pattern matching with variables and constraints
Example Usage
Logic Puzzle
You: "I have 3 people and 3 pets. Alice doesn't own a cat.
Bob owns a mammal but not a dog. Carol doesn't own a cat.
Who owns what?"
Claude: [Creates session, adds constraints, solves using logic server]
Multi-turn Reasoning
Turn 1: "Create a reasoning session"
Turn 2: "Add fact: parent(alice, bob)"
Turn 3: "Add fact: parent(bob, carol)"
Turn 4: "Add rule: grandparent(X,Z) :- parent(X,Y), parent(Y,Z)"
Turn 5: "Who is a grandparent?"
→ Answer: alice
Contract Analysis
You: "Analyze this contract: [paste contract text]"
Claude: [Extracts facts, builds knowledge graph, queries obligations]
→ "Based on formal logic analysis, Acme has 5 obligations:
1. License fee: $120,000/year
2. Support fee: $24,000/year
..."
Running Tests
# All tests
python -m unittest
# Specific test suites
python -m unittest tests.unit.test_sessions
python -m unittest tests.integration.test_mcp_server
# Test with verbose output
python -m unittest discover -v
Performance Optimization
For 50x faster query execution, install janus-swi:
pip install janus-swi
The system automatically uses MQI mode when available (1,152 queries/sec vs 20 queries/sec).
See PROLOG_POOLING.md for advanced performance tuning.
Troubleshooting
Claude Desktop: Server not showing up?
- Verify package is installed:
which logic_server_mcp - Validate JSON config:
cat ~/Library/Application\ Support/Claude/claude_desktop_config.json | python -m json.tool - View logs:
tail -f ~/Library/Logs/Claude/mcp*.log - Fully quit and restart Claude Desktop
Claude Code: Server not connecting?
# Check server status
claude mcp list
# View server details
claude mcp get logic-reasoning
# Remove and re-add
claude mcp remove logic-reasoning
claude mcp add --transport stdio logic-reasoning -- logic_server_mcp
Import errors?
# Install from PyPI
pip install prolog-logic-server
# Or for development
pip install -e .
# Verify installation
python -c "import logic_server.mcp.stateful; print('OK')"
SWI-Prolog not found?
# Verify installation
which swipl
# If not found, install:
brew install swi-prolog # macOS
Documentation
USAGE_GUIDE.md- Complete API reference and MCP setupARCHITECTURE.md- Design notes and implementation detailsCLAUDE_INTEGRATION.md- Detailed Claude Desktop integration guidePROLOG_POOLING.md- Performance optimization with MQIexamples/README_CONTRACT_EXAMPLES.md- Contract analysis examples
Project Structure
src/logic_server/
├── core/
│ ├── solver.py # Core logic query execution
│ ├── mqi_solver.py # High-performance MQI backend
│ ├── session.py # Stateful session management
│ └── prolog_pool.py # Process pooling (optional)
├── mcp/
│ ├── server.py # Stateless MCP server
│ └── stateful.py # Stateful MCP server (recommended)
├── llm/
│ └── clients.py # LLM abstraction (Ollama, OpenAI)
└── cli/
└── main.py # CLI query runner
examples/
├── contract_analysis.py # Contract reasoning demo
├── contract_llm_analysis.py # LLM-based fact extraction
└── puzzles/ # Sample logic puzzles
tests/
├── unit/ # Unit tests
└── integration/ # Integration tests
Advanced Examples
See examples/ directory:
contract_analysis.py- Manual fact extraction and queryingcontract_llm_analysis.py- LLM-powered fact extractionpool_benchmark.py- Performance benchmarkingmqi_session_example.py- High-performance MQI examples
Contributing
# Install dev dependencies
pip install -e ".[dev]"
# Run tests
python -m unittest discover
# Run specific test
python -m unittest tests.unit.test_sessions
License
MIT License - See LICENSE file for details
Support
- Issues: https://github.com/briangu/prolog-logic-server/issues
- Documentation: See docs/ directory
- Examples: See examples/ directory
Metadata
Release files for prolog-logic-server 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| prolog_logic_server-0.1.5.tar.gz | 76.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| prolog_logic_server-0.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 118.5 kB
Release files / prolog_logic_server-0.1.5.tar.gz
| Download URL | prolog_logic_server-0.1.5.tar.gz |
|---|---|
| Size | 76.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5e0b151a52ae8fda8102913b36e984a5f2b5276a7a9fcbba80434bd40a832ef0
|
|
BLAKE2b-256 checksum How to use checksums |
cb395f72d755e4cc9107bfa59c5cf680ac5583fd504f85c327e8fcd3a2a405a2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Release files / prolog_logic_server-0.1.5-py3-none-any.whl
| Download URL | prolog_logic_server-0.1.5-py3-none-any.whl |
|---|---|
| Size | 41.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
263a2208b9a3dedbd885d6e5e710ce3841a55d0d3c41a90c1441a5fc5c017adf
|
|
BLAKE2b-256 checksum How to use checksums |
b021569b84dfab52ac7ff610d73d0c7ca1be85743394fd3383dd89eda9f1ff7d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|