object
mcp_server
MCP (Model Context Protocol) server for Logtalk applications. Exposes Logtalk objects implementing the mcp_tool_protocol and optionally mcp_prompt_protocol and mcp_resource_protocol protocols as MCP tool, prompt, and resource providers over stdio transport with Content-Length framing. Implements the MCP 2025-03-26 specification. Uses the json_rpc library for JSON-RPC 2.0 message handling.
logtalk_load(mcp_server(loader))static, context_switching_calls
MCP specification: Implements the Model Context Protocol 2025-03-26: https://spec.modelcontextprotocol.io/specification/2025-03-26/
Transport: Uses stdio (standard input/output) with Content-Length header framing as defined by the MCP specification.
Version negotiation: During initialization, the server performs protocol version negotiation. The client sends its highest supported version in the
initializerequest. The server selects the highest version it supports that is compatible (i.e., less than or equal to the client version). If no compatible version exists, the server responds with error-32602(“Unsupported protocol version”) including adatafield with the list of supported versions.Capabilities: Supports the tools capability (
tools/listandtools/call). Optionally supports the prompts capability (prompts/listandprompts/get) when the application declares it viacapabilities/1and implementsmcp_prompt_protocol. Optionally supports the resources capability (resources/listandresources/read) when the application declares it viacapabilities/1and implementsmcp_resource_protocol. Optionally supports the elicitation capability (elicitation/create) when the application declares it viacapabilities/1.Tool discovery: Tools are discovered from objects implementing the
mcp_tool_protocol. Tool metadata (descriptions, parameter schemas) is derived frominfo/2andmode/2directives.Prompt discovery: Prompts are discovered from objects implementing the
mcp_prompt_protocol. Prompt metadata (names, descriptions, arguments) is declared via theprompts/1predicate.Resource discovery: Resources are discovered from objects implementing the
mcp_resource_protocol. Resource metadata (URIs, names, descriptions, MIME types) is declared via theresources/1predicate.Auto-dispatch: When an object does not define
tool_call/3ortool_call/4for a tool, the server auto-dispatches: it calls the predicate as a message to the object, collects output-mode arguments, and returns them as text content.Elicitation: When the application declares
elicitationin itscapabilities/1, the server constructs an elicit closure and passes it totool_call/4. The closure sendselicitation/createrequests to the client and reads back the user response. This enables interactive tools that can ask the user questions during execution.Error handling: Predicate failures and exceptions both result in MCP tool-level errors with
isErrorset totrue.
Public predicates
start/2
Starts the MCP server with the given server name and application object. The application object must implement the mcp_tool_protocol protocol. Reads JSON-RPC messages from standard input and writes responses to standard output using Content-Length framing. Blocks until the client disconnects or an exit signal is received.
staticstart(Name,Application)start(+atom,+object_identifier) - onestart/3
Starts the MCP server with the given server name, application object, and options. Currently supported options: server_version(Version) to set the server version (default '1.0.0'), server_title(Title) to set the server display title (default '', omitted when empty).
staticstart(Name,Application,Options)start(+atom,+object_identifier,+list) - onestart/4
Starts the MCP server with custom input and output streams. Useful for testing or non-stdio transports. Uses default options.
staticstart(Name,Application,Input,Output)start(+atom,+object_identifier,+stream,+stream) - onestart/5
Starts the MCP server with custom input and output streams and options.
staticstart(Name,Application,Input,Output,Options)start(+atom,+object_identifier,+stream,+stream,+list) - oneelicit_request/5
Sends an elicitation/create request to the MCP client and reads the response. Input and Output are the I/O streams. Message is the prompt text (an atom). RequestedSchema is a curly-term JSON Schema describing the input to request from the user (e.g., {type-object, properties-{answer-{type-string, enum-[yes, no]}}, required-[answer]}). Answer is unified with accept(Content) (where Content is the user response as a curly-term), decline, or cancel. This predicate is typically not called directly but through the elicit closure passed to tool_call/4.
staticelicit_request(Input,Output,Message,RequestedSchema,Answer)elicit_request(+stream,+stream,+atom,+compound,--compound) - oneProtected predicates
(no local declarations; see entity ancestors if any)
Private predicates
initialized_/0
dynamicapplication_/1
dynamicserver_name_/1
dynamicserver_version_/1
dynamicserver_title_/1
dynamicelicit_counter_/1
dynamicapplication_capabilities_/1
dynamicOperators
(none)