Writing a Model Context Protocol (MCP) Server in Common Lisp - macOS Only
The Model Context Protocol (MCP) is an open standard that lets LLM clients call tools that run outside the model. Claude Code, Claude Desktop, and many IDE agents act as MCP clients. When you write an MCP server, every one of these clients can use your tools with no client code changes.
In this chapter we write an MCP server from scratch in about 250 lines of Common Lisp. The server exposes the macOS automation functions from the previous chapter as tools, so an LLM can count unread mail, list today’s calendar events, add reminders, and read or send iMessages.
We use no MCP library. The protocol is small, and writing it ourselves shows how it works. The only dependencies are UIOP and the JSON library jzon.
Note 1: The tools in this chapter run only on macOS. The server code in mcp-server.lisp is portable.
Note 2: The source code is in the directory src/MCP_common_lisp.
How MCP Works
An MCP server can use one of two transports: stdio or HTTP. We use stdio, the simpler choice for local tools:
- The client starts the server as a child process.
- The client writes JSON-RPC 2.0 messages to the server’s stdin, one message per line.
- The server writes one JSON-RPC message per line to stdout.
- The server may write log text to stderr. Stdout must carry nothing but protocol messages.
JSON-RPC has two kinds of messages from the client. A request has an id field and needs a response with the same id. A notification has no id, and the server must not respond to it.
A session has three steps:
- The client sends an
initializerequest with the protocol version it wants. The server replies with the version it will use, its capabilities, and its name. - The client sends the
notifications/initializednotification. - The client sends
tools/listto learn which tools exist, thentools/calleach time the model wants to run a tool.
Each tool has a name, a description, and a JSON Schema that describes its arguments. The model reads the description and schema to decide when to call the tool and what arguments to pass.
Project Structure
| File | Purpose |
|---|---|
package.lisp |
The :mac package (copied from the macOS chapter) |
osascript.lisp |
AppleScript bridge for Mail, Calendar, Messages, and Reminders (copied) |
read-recent-imessages.lisp |
Reads iMessage history from SQLite (copied) |
safari-login.lisp |
Safari automation (copied, not exposed as a tool) |
mcp-package.lisp |
The :mcp-server package |
mcp-server.lisp |
JSON-RPC handling, the tool registry, and the deftool macro |
mac-tools.lisp |
MCP tool definitions that call the :mac functions |
mcp-common-lisp.asd |
ASDF system definition |
run-server.sh |
Starts the server so that stdout stays clean |
test-session.sh |
Sends a sample session to the server |
The previous chapter explains the four copied files, so we don’t list them again here.
mcp-common-lisp.asd
1 ;;;; mcp-common-lisp.asd -- an MCP server that exposes macOS automation tools.
2
3 (asdf:defsystem #:mcp-common-lisp
4 :description "Model Context Protocol (MCP) server over stdio with macOS tools."
5 :author "Mark Watson"
6 :license "Apache 2"
7 :depends-on (#:uiop #:com.inuoe.jzon)
8 :serial t
9 :components ((:file "package")
10 (:file "osascript")
11 (:file "read-recent-imessages")
12 (:file "safari-login")
13 (:file "mcp-package")
14 (:file "mcp-server")
15 (:file "mac-tools")))
mcp-package.lisp
We use a local nickname so the code can write jzon:parse instead of the full package name com.inuoe.jzon:
1 ;;;; mcp-package.lisp -- package for the MCP server.
2
3 (defpackage #:mcp-server
4 (:use #:cl)
5 (:local-nicknames (#:jzon #:com.inuoe.jzon))
6 (:export #:deftool
7 #:start
8 #:handle-message
9 #:*tools*))
The Server: mcp-server.lisp
We walk through mcp-server.lisp in five parts: settings, JSON helpers, the tool registry, JSON-RPC errors, and the MCP methods with the main loop.
Settings
1 (in-package #:mcp-server)
2
3 (defparameter *server-name* "common-lisp-macos")
4 (defparameter *server-version* "0.1.0")
5
6 ;; Newest first. We echo the client's version if we support it,
7 ;; otherwise we answer with our newest version.
8 (defparameter *supported-protocol-versions*
9 '("2025-11-25" "2025-06-18" "2025-03-26" "2024-11-05"))
MCP names protocol versions by date. The client asks for a version in its initialize request. If we support that version we send it back. If not, we reply with our newest version, and the client decides whether it can work with it. The parts of the protocol this server uses have not changed across these four versions.
JSON Helpers
The jzon library maps JSON objects to equal hash tables, JSON arrays to vectors, true to t, false to nil, and null to the symbol null. Using hash tables for objects means an empty object {} and an empty array [] stay distinct, which matters because MCP requires both in different places.
1 (defun obj (&rest keys-and-values)
2 "Build a JSON object from alternating string keys and values."
3 (let ((table (make-hash-table :test #'equal)))
4 (loop for (key value) on keys-and-values by #'cddr
5 do (setf (gethash key table) value))
6 table))
7
8 (defun json-get (object key &optional default)
9 (if (hash-table-p object)
10 (gethash key object default)
11 default))
12
13 (defun log-message (format-control &rest args)
14 (format *error-output* "~&[mcp] ~?~%" format-control args)
15 (finish-output *error-output*))
obj builds a JSON object, so (obj "type" "text" "text" "hello") becomes {"type":"text","text":"hello"}. json-get reads a field and returns the default when the value is not an object, which saves a type check each time we read a field the client may have left out. log-message writes to stderr. The ~? directive takes a format string and its argument list, so log-message accepts the same arguments as format.
The Tool Registry and the deftool Macro
Each tool is a struct in a hash table keyed by the tool name:
1 (defstruct tool
2 name ; string sent to the client, e.g. "add_reminder"
3 description
4 input-schema ; JSON Schema object (hash table)
5 function) ; function of one argument: the arguments hash table
6
7 (defvar *tools* (make-hash-table :test #'equal)
8 "Map from tool name to TOOL struct.")
9
10 (defun lisp-name->json-name (symbol)
11 (substitute #\_ #\- (string-downcase (symbol-name symbol))))
Lisp names use hyphens and JSON names in MCP tools use underscores, so lisp-name->json-name turns task-name into task_name.
Arguments come from the LLM, so we treat them as untrusted input. parameter-value fetches one argument, applies a default when the argument is absent, and checks its type:
1 (defun json-type-p (value json-type)
2 (cond ((string= json-type "string") (stringp value))
3 ((string= json-type "integer") (integerp value))
4 ((string= json-type "number") (realp value))
5 ((string= json-type "boolean") (or (eq value t) (eq value nil)))
6 (t t)))
7
8 (defun parameter-value (arguments json-name json-type requiredp default)
9 "Fetch and type check one tool argument. An LLM writes these values, so
10 never trust them: a string where we expect an integer can turn into SQL
11 or AppleScript injection further down."
12 (multiple-value-bind (value presentp) (gethash json-name arguments)
13 (cond ((not presentp)
14 (if requiredp
15 (error "Missing required argument: ~A" json-name)
16 default))
17 ((not (json-type-p value json-type))
18 (error "Argument ~A must be of type ~A" json-name json-type))
19 (t value))))
The type check matters here. The function read-recent-imessages from the previous chapter pastes its limit argument into SQL text. If a model sent the string "5; DROP TABLE message" as the limit, the type check rejects it before it reaches SQLite.
make-input-schema builds the JSON Schema that tools/list sends to the client:
1 (defun make-input-schema (parameters)
2 (let ((properties (make-hash-table :test #'equal))
3 (required-names '()))
4 (dolist (parameter parameters)
5 (destructuring-bind (var json-type description &key required default)
6 parameter
7 (declare (ignore default))
8 (let ((json-name (lisp-name->json-name var)))
9 (setf (gethash json-name properties)
10 (obj "type" json-type "description" description))
11 (when required (push json-name required-names)))))
12 (obj "type" "object"
13 "properties" properties
14 "required" (coerce (nreverse required-names) 'vector))))
Note the name required-names. My first version called this list required, and the &key required in destructuring-bind shadowed it, so push added names to the wrong variable and every schema came out with an empty required array. The test session at the end of this chapter caught the bug.
The deftool macro ties these pieces together. A tool definition lists its parameters in the form (var json-type description &key required default), then a description string, then a body that returns the result text:
1 (defmacro deftool (name (&rest parameters) description &body body)
2 "Define an MCP tool. Each parameter is
3 (VAR JSON-TYPE DESCRIPTION &key REQUIRED DEFAULT)
4 VAR is bound to the argument value inside BODY. BODY returns the tool's
5 text result; a non-string result is converted with PRINC-TO-STRING."
6 (let ((arguments (gensym "ARGUMENTS")))
7 `(setf (gethash ,(lisp-name->json-name name) *tools*)
8 (make-tool
9 :name ,(lisp-name->json-name name)
10 :description ,description
11 :input-schema (make-input-schema ',parameters)
12 :function
13 (lambda (,arguments)
14 (declare (ignorable ,arguments))
15 (let* ,(loop for (var json-type nil . options) in parameters
16 collect `(,var (parameter-value
17 ,arguments
18 ,(lisp-name->json-name var)
19 ,json-type
20 ,(getf options :required)
21 ,(getf options :default))))
22 ,@body))))))
The macro computes the tool name at expansion time. It generates a lambda that binds each parameter variable to a checked value from the arguments hash table, then runs the body. The gensym keeps the arguments variable from clashing with a parameter that a tool author might name arguments. You can see the expansion with macroexpand-1:
1 (macroexpand-1
2 '(deftool add-reminder ((task-name "string" "Text" :required t))
3 "Create a reminder."
4 (mac:add-reminder task-name)))
This prints (after reformatting):
1 (SETF (GETHASH "add_reminder" *TOOLS*)
2 (MAKE-TOOL
3 :NAME "add_reminder"
4 :DESCRIPTION "Create a reminder."
5 :INPUT-SCHEMA (MAKE-INPUT-SCHEMA '((TASK-NAME "string" "Text" :REQUIRED T)))
6 :FUNCTION
7 (LAMBDA (#:ARGUMENTS123)
8 (DECLARE (IGNORABLE #:ARGUMENTS123))
9 (LET* ((TASK-NAME (PARAMETER-VALUE #:ARGUMENTS123 "task_name"
10 "string" T NIL)))
11 (MAC:ADD-REMINDER TASK-NAME)))))
JSON-RPC Errors
JSON-RPC defines numeric error codes. We use four of them:
| Code | Meaning |
|---|---|
| -32700 | Parse error: the line is not valid JSON |
| -32600 | Invalid request: no method field |
| -32601 | Method not found |
| -32602 | Invalid params, such as an unknown tool name |
| -32603 | Internal error |
A condition class carries the code from deep in the call stack up to the code that writes the response:
1 (define-condition json-rpc-error (error)
2 ((code :initarg :code :reader json-rpc-error-code)
3 (message :initarg :message :reader json-rpc-error-message))
4 (:report (lambda (condition stream)
5 (format stream "JSON-RPC error ~D: ~A"
6 (json-rpc-error-code condition)
7 (json-rpc-error-message condition)))))
8
9 (defun rpc-error (code format-control &rest args)
10 (error 'json-rpc-error :code code
11 :message (apply #'format nil format-control args)))
12
13 (defun success-response (id result)
14 (obj "jsonrpc" "2.0" "id" id "result" result))
15
16 (defun error-response (id code message)
17 (obj "jsonrpc" "2.0" "id" id
18 "error" (obj "code" code "message" message)))
The MCP Methods
initialize picks the protocol version and declares that this server supports tools. Setting listChanged to false tells the client our tool list never changes during a session:
1 (defun handle-initialize (params)
2 (let* ((requested (json-get params "protocolVersion"))
3 (version (if (member requested *supported-protocol-versions*
4 :test #'equal)
5 requested
6 (first *supported-protocol-versions*))))
7 (log-message "initialize: client asked for ~A, using ~A" requested version)
8 (obj "protocolVersion" version
9 "capabilities" (obj "tools" (obj "listChanged" nil))
10 "serverInfo" (obj "name" *server-name* "version" *server-version*))))
tools/list returns every tool sorted by name:
1 (defun handle-tools-list ()
2 (let ((tools (loop for tool being the hash-values of *tools*
3 collect (obj "name" (tool-name tool)
4 "description" (tool-description tool)
5 "inputSchema" (tool-input-schema tool)))))
6 (obj "tools" (coerce (sort tools #'string< :key (lambda (tool) (gethash "name" tool)))
7 'vector))))
tools/call runs a tool. MCP separates two kinds of failure. A request for an unknown tool is a protocol error, so we signal json-rpc-error. A failure inside a tool, such as a missing argument or an AppleScript error, goes back as a normal result with isError set to true. The model sees that error text and can fix its arguments and try again:
1 (defun tool-result (text &optional errorp)
2 (obj "content" (vector (obj "type" "text" "text" text))
3 "isError" errorp))
4
5 (defun handle-tools-call (params)
6 (let* ((name (json-get params "name"))
7 (arguments (json-get params "arguments"))
8 (tool (and (stringp name) (gethash name *tools*))))
9 (unless tool
10 (rpc-error -32602 "Unknown tool: ~A" name))
11 (unless (hash-table-p arguments)
12 (setf arguments (make-hash-table :test #'equal)))
13 (log-message "tools/call ~A" name)
14 ;; MCP reports failures inside a tool as a normal result with isError
15 ;; true, so the model can read the message and try again.
16 (handler-case
17 (let ((result (funcall (tool-function tool) arguments)))
18 (tool-result (if (stringp result) result (princ-to-string result))))
19 (error (condition)
20 (log-message "tool ~A failed: ~A" name condition)
21 (tool-result (princ-to-string condition) t)))))
Because the osascript-error condition from the previous chapter has a :report function, princ-to-string turns it into the real AppleScript diagnostic, for example osascript failed (exit 1): ... Not authorized to send Apple events to Mail. The model then gets a message it can pass on to the user.
dispatch maps a method name to its handler, and handle-message processes one parsed message:
1 (defun dispatch (method params)
2 (cond ((equal method "initialize") (handle-initialize params))
3 ((equal method "ping") (obj))
4 ((equal method "tools/list") (handle-tools-list))
5 ((equal method "tools/call") (handle-tools-call params))
6 (t (rpc-error -32601 "Method not found: ~A" method))))
7
8 (defun handle-message (message)
9 "Process one parsed JSON-RPC MESSAGE. Return the response object, or NIL
10 for notifications, which never get a response."
11 (multiple-value-bind (id requestp)
12 (if (hash-table-p message) (gethash "id" message) (values nil nil))
13 (let ((method (json-get message "method"))
14 (params (json-get message "params")))
15 (cond ((not (stringp method))
16 (and requestp (error-response id -32600 "Invalid request")))
17 ((not requestp)
18 (log-message "notification ~A" method)
19 nil)
20 (t
21 (handler-case (success-response id (dispatch method params))
22 (json-rpc-error (condition)
23 (error-response id (json-rpc-error-code condition)
24 (json-rpc-error-message condition)))
25 (error (condition)
26 (error-response id -32603 (princ-to-string condition)))))))))
We use the second value of gethash to tell a request from a notification. Testing the id value itself would fail for a request whose id is 0 or false; the presence of the key is what counts.
The Main Loop
1 (defun handle-line (line)
2 (let ((message (handler-case (jzon:parse line)
3 (error () (return-from handle-line
4 (error-response 'null -32700 "Parse error"))))))
5 (handle-message message)))
6
7 (defun start (&key (input *standard-input*) (output *standard-output*))
8 "Read JSON-RPC messages from INPUT, one per line, until end of file."
9 (log-message "~A ~A started with ~D tools"
10 *server-name* *server-version* (hash-table-count *tools*))
11 ;; Anything printed to *standard-output* would corrupt the protocol
12 ;; stream, so point it at stderr while tools run.
13 (let ((*standard-output* *error-output*))
14 (loop for line = (read-line input nil)
15 while line
16 unless (zerop (length (string-trim '(#\Space #\Tab #\Return) line)))
17 do (let ((response (handle-line line)))
18 (when response
19 ;; jzon escapes newlines inside strings, so each
20 ;; message fits on one line as the transport requires.
21 (write-line (jzon:stringify response) output)
22 (finish-output output)))))
23 (log-message "stdin closed, exiting"))
start grabs the real stdout as output before it binds *standard-output* to stderr. After that, a stray print in any tool lands in the log and cannot break the protocol. finish-output after each message matters: without it SBCL buffers the response and the client waits forever. When the client closes stdin, read-line returns nil and the server exits.
The Tools: mac-tools.lisp
With deftool in place, each tool takes a few lines:
1 ;;;; mac-tools.lisp -- MCP tools built on the :mac osascript functions.
2
3 (in-package #:mcp-server)
4
5 (deftool mail-unread-count ()
6 "Return the number of unread messages in the macOS Mail inbox."
7 (format nil "~D unread messages" (mac:mail-unread-count)))
8
9 (deftool todays-events ()
10 "List the titles of today's events from all calendars in macOS Calendar."
11 (let ((events (remove "" (mac:get-todays-events) :test #'string=)))
12 (if events
13 (format nil "~{~A~^~%~}" events)
14 "No events today.")))
15
16 (deftool add-reminder ((task-name "string" "Text of the new reminder" :required t))
17 "Create a new reminder in the macOS Reminders app."
18 (format nil "Created reminder ~S (id ~A)" task-name (mac:add-reminder task-name)))
19
20 (deftool read-recent-imessages
21 ((limit "integer" "How many recent messages to return (1 to 50)" :default 5))
22 "Read the most recent iMessages as JSON objects with is_from_me, sender,
23 and text fields. Requires Full Disk Access for the process running the server."
24 (unless (<= 1 limit 50)
25 (error "limit must be between 1 and 50"))
26 (mac:read-recent-imessages limit))
27
28 (deftool send-imessage
29 ((phone-number "string" "Recipient phone number or Apple ID email" :required t)
30 (message "string" "Text to send" :required t))
31 "Send an iMessage using the macOS Messages app."
32 (mac:send-imessage phone-number message)
33 (format nil "Sent message to ~A" phone-number))
Write tool descriptions for the model, not for a human reader. The model chooses tools from these strings, so say what the tool does and what it needs.
todays-events removes empty strings because get-todays-events splits an empty AppleScript result into a list holding one empty string. read-recent-imessages adds a range check on top of the type check so a model can’t ask for the whole message history at once.
I left safari-login out of the tool list. It takes a password, and a password should never pass through an LLM conversation.
Starting the Server: run-server.sh
Clients start the server with a shell script:
1 #!/bin/sh
2 # Start the MCP server on stdio. Stdout carries only JSON-RPC messages.
3
4 DIR="$(cd "$(dirname "$0")" && pwd)"
5
6 exec sbcl --noinform --non-interactive \
7 --eval "(asdf:load-asd \"$DIR/mcp-common-lisp.asd\")" \
8 --eval "(ql:quickload :mcp-common-lisp :silent t)" \
9 --eval "(mcp-server:start)"
The script assumes your ~/.sbclrc loads Quicklisp, as the Quicklisp installer sets up. asdf:load-asd tells ASDF where the system lives, so the script works from any directory. The :silent t option keeps Quicklisp’s progress messages off stdout, and --noinform drops the SBCL banner.
Your ~/.sbclrc must not print to stdout either. I once had (ql:quickload :cffi) in mine, and its To load "cffi": message reached the client before the server started. A client reads such a line as a broken JSON message. To check your setup, run:
1 echo | sbcl --noinform --non-interactive
This command should print nothing.
Testing Without an LLM
You don’t need an LLM client to test an MCP server. Since the protocol is text lines on stdin and stdout, a shell here-document can play the client. The script test-session.sh sends a session that exercises each code path without touching a macOS app:
1 #!/bin/sh
2 # Send a short MCP session to the server and print its replies.
3 # These calls do not touch any macOS app.
4
5 DIR="$(cd "$(dirname "$0")" && pwd)"
6
7 "$DIR/run-server.sh" 2>/dev/null <<'EOF'
8 {"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}
9 {"jsonrpc":"2.0","method":"notifications/initialized"}
10 {"jsonrpc":"2.0","id":2,"method":"tools/list"}
11 {"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"read_recent_imessages","arguments":{"limit":"5; DROP TABLE message"}}}
12 {"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"add_reminder","arguments":{}}}
13 {"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"no_such_tool","arguments":{}}}
14 {"jsonrpc":"2.0","id":6,"method":"ping"}
15 not json
16 EOF
The output, with the long tools/list response cut short:
1 $ ./test-session.sh
2 {"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2025-06-18","capabilities":{"tools":{"listChanged":false}},"serverInfo":{"name":"common-lisp-macos","version":"0.1.0"}}}
3 {"jsonrpc":"2.0","id":2,"result":{"tools":[{"name":"add_reminder","description":"Create a new reminder in the macOS Reminders app.","inputSchema":{"type":"object","properties":{"task_name":{"type":"string","description":"Text of the new reminder"}},"required":["task_name"]}},{"name":"mail_unread_count", ...
4 {"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"Argument limit must be of type integer"}],"isError":true}}
5 {"jsonrpc":"2.0","id":4,"result":{"content":[{"type":"text","text":"Missing required argument: task_name"}],"isError":true}}
6 {"jsonrpc":"2.0","id":5,"error":{"code":-32602,"message":"Unknown tool: no_such_tool"}}
7 {"jsonrpc":"2.0","id":6,"result":{}}
8 {"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}
Check each line against the protocol rules:
- Seven requests produced seven responses. The
notifications/initializedline produced none. - The server echoed the client’s protocol version
2025-06-18. - The injection attempt and the missing argument came back as tool results with
isErrortrue. - The unknown tool came back as a JSON-RPC error with code -32602.
- The line
not jsongot a parse error with anullid, since the server could not read an id from it.
To test a real tool, send one tools/call line. Here I keep stderr so the log lines show:
1 $ echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"todays_events","arguments":{}}}' | ./run-server.sh
2 [mcp] common-lisp-macos 0.1.0 started with 5 tools
3 [mcp] tools/call todays_events
4 {"jsonrpc":"2.0","id":1,"result":{"content":[{"type":"text","text":"Mark sutures"}],"isError":false}}
5 [mcp] stdin closed, exiting
The first time a tool talks to an app, macOS asks whether the process may control that app. The previous chapter covers these Automation and Full Disk Access settings.
For interactive testing, the MCP project provides a web-based inspector that lists your tools and lets you call them from a form:
1 npx @modelcontextprotocol/inspector /path/to/src/MCP_common_lisp/run-server.sh
Using the Server from Claude Code and Claude Desktop
To add the server to Claude Code, give it a name and the command that starts it:
1 claude mcp add macos-lisp -- /path/to/src/MCP_common_lisp/run-server.sh
For Claude Desktop, add an entry to ~/Library/Application Support/Claude/claude_desktop_config.json and restart the app:
1 {
2 "mcpServers": {
3 "macos-lisp": {
4 "command": "/path/to/src/MCP_common_lisp/run-server.sh"
5 }
6 }
7 }
Replace /path/to with the location of your copy of the book’s source code. You can now ask questions like “What’s on my calendar today?” or “Remind me to renew my passport”, and the model calls the matching tool.
macOS applies its privacy settings to the app that starts the server, so you may need to grant Automation and Full Disk Access to Claude Desktop or to your terminal, depending on the client.
The send_imessage tool sends real messages. Most MCP clients ask you to approve each tool call by default; keep that setting on for this tool.
Adding Your Own Tools
To add a tool, write a deftool form in mac-tools.lisp or in a new file listed in the .asd file. Here is a tool that gives the model the current date and time, which LLMs do not know on their own:
1 (deftool current-time ()
2 "Return the current local date and time."
3 (multiple-value-bind (second minute hour day month year)
4 (get-decoded-time)
5 (format nil "~4,'0D-~2,'0D-~2,'0D ~2,'0D:~2,'0D:~2,'0D"
6 year month day hour minute second)))
Since *tools* is a hash table that tools/list reads on each call, a tool you define in a live Lisp image appears the next time a client lists the tools. With a REPL attached to a running server, you can add and fix tools without a restart.
Wrap Up
An MCP server is a loop that reads JSON-RPC lines from stdin and writes JSON-RPC lines to stdout. In Common Lisp the protocol code fits in one file, and a short macro turns any Lisp function into a tool that every MCP client can call.
The points to remember:
- Keep stdout clean. Send logs to stderr, load systems with
:silent t, and keep~/.sbclrcquiet. - Call
finish-outputafter each response. - Never answer a notification.
- Report tool failures as results with
isErrortrue, and protocol failures as JSON-RPC errors. - Check the type of every argument. The model writes them, and the values may end up inside SQL or AppleScript.
The same pattern works for any Lisp code in this book. Good next steps would be tools that run SPARQL queries against the RDF store from the Knowledge Graph chapters, or tools that search a document collection with the RAG code from Part 7.