This project implements an MCP (Multi-Component Protocol) server that allows clients, potentially driven by LLMs, to interact with a Prolog knowledge base. It uses Node.js, Express, and the trealla library to provide a Trealla Prolog environment.
- Assert new facts and rules into the Prolog database.
- Query the Prolog database and retrieve variable bindings.
- Retract specific clauses from the database.
- List all user-defined clauses currently in the database.
- Powered by
trealla, a WASM-based Trealla Prolog interpreter.
This project is licensed under the MIT License. See the LICENSE file for details.
server.js: The main Node.js Express server application.gemini_prolog_client.py: A Python client script demonstrating how to interact with the server.package.json: Defines project dependencies and scripts.README.md: This file.
- Clone the repository (if you haven't already):
git clone https://github.com/Helvia/mcp-trealla.git cd mcp-trealla - Install Node.js dependencies:
npm install
- Install Python client dependencies (for
gemini_prolog_client.py): It's recommended to use a virtual environment for Python projects.You will also need to set thepython -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate` pip install requests google-generativeai
GEMINI_API_KEYenvironment variable for the Python client to work:export GEMINI_API_KEY='your_actual_gemini_api_key'
Start the Node.js server:
npm startThe server will typically run on http://localhost:3000.
All API endpoints are session-specific and require a sessionId path parameter. The sessionId can be any unique string generated by the client to identify a distinct Prolog session.
The server exposes the following HTTP endpoints:
- Endpoint:
POST /session/:sessionId/prolog/assert - Description: Adds a single Prolog clause (fact, rule, or directive) to the knowledge base for the specified session.
- Request Body: JSON object
{ "clause": "father(john, mary)." }clause: A string containing a single complete Prolog clause. It should end with a period. Directives (e.g.,:- dynamic(predicate/arity).) are processed by the server to modify the Prolog environment for the session. Facts and rules are asserted into the database.
- Success Response (200 OK):
{ "success": true, "message": "Clause asserted: father(john, mary)." } - Error Response (e.g., 400 Bad Request):
{ "success": false, "message": "A Prolog clause to assert is required...", "error": "Clause is required" }
- Endpoint:
POST /session/:sessionId/prolog/query - Description: Executes a Prolog query within the specified session and returns solutions.
- Request Body: JSON object
{ "query": "father(X, mary)" }query: A string containing a valid Prolog query. The server will automatically add a trailing period if one is not present.
- Success Response (200 OK):
Returns an empty array
{ "success": true, "answers": [ { "X": "john" } ] }[]foranswersif the query fails or has no solutions (for non-ground queries). For ground queries that succeed,answerswill be[{}]. - Error Response (e.g., 400 Bad Request):
{ "success": false, "message": "A Prolog query is required...", "error": "Query is required" }
- Endpoint:
GET /session/:sessionId/prolog/listing - Description: Retrieves a textual listing of all user-defined clauses in the current session's knowledge base. System-internal predicates are filtered out.
- Request Body: None
- Success Response (200 OK):
If no user-defined clauses exist,
{ "success": true, "listing": "father(john,mary).\nlikes(jane,apples).\nmother(jane,mary).\nancestor(A,B) :-\n mother(A,B).\nancestor(A,B) :-\n father(A,B)." }listingwill be an empty string or contain only directives like:- dynamic.
- Endpoint:
POST /session/:sessionId/prolog/retract - Description: Removes the first clause in the specified session's knowledge base that unifies with the provided term.
- Request Body: JSON object
{ "clause": "likes(jane,apples)" }clause: A string representing a Prolog term (fact or rule head). The server will automatically add a trailing period if one is not present. Variables can be used if the intent is to retract the first matching instance.
- Success Response (200 OK, if a clause was retracted):
{ "success": true, "message": "Clause retracted: likes(jane,apples)" } - Not Found Response (404 Not Found, if no matching clause was found):
{ "success": false, "message": "No matching clause found for retract: likes(jane,apples)" } - Error Response (e.g., 400 Bad Request):
{ "success": false, "message": "A Prolog clause to retract is required...", "error": "Clause (Prolog term string) for retract is required" }
The gemini_prolog_client.py script provides an interactive command-line interface to communicate with the Prolog server using natural language. It leverages the Google Gemini API to translate your commands into Prolog actions (assert, retract, list, query).
- Ensure the MCP Prolog server is running (see "Running the Server").
- Ensure Python dependencies are installed (see "Setup" section), including
requestsandgoogle-generativeai. - Set your Gemini API Key: The client requires the
GEMINI_API_KEYenvironment variable to be set.Replaceexport GEMINI_API_KEY='your_actual_gemini_api_key'
'your_actual_gemini_api_key'with your valid key. - Run the client:
You will be prompted with
python3 gemini_prolog_client.py
Your message >. You can then type natural language commands like:- "Assert father(john, mary) and mother(lisa, john)."
- "List the knowledge base."
- "Retract father(john, mary)."
- "Retract all facts."
- "Who is mary's father?" (Query functionality relies on Gemini's interpretation to form a Prolog query)
- Type
quitto exit the client.
The client will print debug information showing Gemini's interpretation of your command and the server's responses.
- The server uses
treallawhich runs Trealla Prolog in a WebAssembly environment. - The Python client (
gemini_prolog_client.py) uses the Google Gemini API to interpret natural language commands and translate them into Prolog actions for the server. It demonstrates a more advanced integration than simple placeholder functions.