Skip to content

Commit 4a3504c

Browse files
Slim/server (#33)
Add server mode
1 parent efe8567 commit 4a3504c

19 files changed

+1019
-326
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
/tools_vector_store/.venv/
1818
/tools_vector_store/tools.db
1919
/tools_vector_store/chroma_db/
20+
/docker-mcp-server.out

deps-lock.json

Lines changed: 332 additions & 265 deletions
Large diffs are not rendered by default.

deps.edn

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
selmer/selmer {:mvn/version "1.12.61"}
1212
cheshire/cheshire {:mvn/version "5.13.0"}
1313
org.clojure/core.async {:mvn/version "1.6.681"}
14-
org.babashka/http-client {:mvn/version "0.4.12"}}
14+
org.babashka/http-client {:mvn/version "0.4.12"}
15+
com.taoensso/timbre {:mvn/version "5.2.1"}
16+
io.github.slimslenderslacks/lsp4clj {:git/sha "995abcdbfebfa7ef6550625fe1ef3ca2c7683292"}
17+
funcool/promesa {:mvn/version "9.0.470"}}
1518
:aliases {:main {:main-opts ["-m" "docker.main"]}
1619
:build {:ns-default build
1720
:deps {io.github.clojure/tools.build {:git/tag "v0.9.5" :git/sha "24f2894"}}}}}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
description: |
3+
Provide a detailed description, analysis, or annotation of a given Dockerfile,
4+
explaining its structure and functionality.
5+
6+
Synonyms: explain my Dockerfile, annotate this Dockerfile...
7+
8+
This tool can explain a pre-provided Dockerfile but it can also fetch the Dockerfile from the user's workspace.
9+
tools:
10+
- name: cat_file
11+
description: fetch a file
12+
parameters:
13+
type: object
14+
properties:
15+
path:
16+
type: string
17+
description: Path of the folder to delete
18+
container:
19+
image: vonwig/bash_alpine
20+
command:
21+
- "cat {{path|safe}}"
22+
- name: findutils-by-name
23+
description: find files in a project by name
24+
parameters:
25+
type: object
26+
properties:
27+
glob:
28+
type: string
29+
description: the glob pattern for files that should be found
30+
container:
31+
image: vonwig/findutils:latest
32+
command:
33+
- find
34+
- .
35+
- -name
36+
- "{{glob|safe}}"
37+
prompt-format: "django"
38+
---
39+
40+
# prompt user
41+
42+
Start by fetching the ./Dockerfile in the project root. If there is no Dockerfile, then search for other files named 'Dockerfile' in the project and ask which one we want to explain.
43+
44+
After fetching the Dockerfile contents, explain the Dockerfile line by line.
45+
46+
Requirements:
47+
+ Each command MUST pre prefixed with its line number.
48+
+ For each command, add links to the documentation if necessary.
49+
+ Use a new paragraph for links to the documentation.
50+
+ {% tip "How can I optimize my Dockerfile?" %}
51+
+ {% tip "Can you rate my Dockerfile?" %}
52+
53+
Sample final output:
54+
55+
**01: FROM golang**
56+
57+
directly explain what it does, without prefix...
58+
59+
[See documentation](https://docs.docker.com/engine/reference/builder/#command)
60+
61+
**05: CMD something**
62+
63+
directly explain what it does, without prefix...
64+
65+
[See documentation](https://docs.docker.com/engine/reference/builder/#command)
66+

prompts/examples/mcp-sqlite.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
description: |
3+
A prompt to seed the database with initial data and demonstrate what you can do with an SQLite MCP Server + Claude
4+
tools:
5+
- name: read-query
6+
description: Execute a SELECT query on the SQLite database
7+
container:
8+
image: vonwig/sqlite:latest
9+
command:
10+
- "{{database}}"
11+
- "{{sql}}"
12+
- name: write-query
13+
description: Execute an INSERT, UPDATE, or DELETE query on the SQLite database
14+
- name: create-table
15+
description: Create a new table in the SQLite database
16+
- name: list-tables
17+
description: List all tables in the SQLite database
18+
- name: describe-table
19+
description: Get the schema information for a specific table
20+
- name: append-insight
21+
description: Add a business insight to the memo
22+
---
23+
24+
# prompt user
25+
26+
The assistants goal is to walkthrough an informative demo of MCP. To demonstrate the Model Context Protocol (MCP) we will leverage this example server to interact with an SQLite database.
27+
It is important that you first explain to the user what is going on. The user has downloaded and installed the SQLite MCP Server and is now ready to use it.
28+
They have selected the MCP menu item which is contained within a parent menu denoted by the paperclip icon. Inside this menu they selected an icon that illustrates two electrical plugs connecting. This is the MCP menu.
29+
Based on what MCP servers the user has installed they can click the button which reads: 'Choose an integration' this will present a drop down with Prompts and Resources. The user has selected the prompt titled: 'mcp-demo'.
30+
This text file is that prompt. The goal of the following instructions is to walk the user through the process of using the 3 core aspects of an MCP server. These are: Prompts, Tools, and Resources.
31+
They have already used a prompt and provided a topic. The topic is: {{topic}}. The user is now ready to begin the demo.
32+
Here is some more information about mcp and this specific mcp server:
33+
<mcp>
34+
Prompts:
35+
This server provides a pre-written prompt called "mcp-demo" that helps users create and analyze database scenarios. The prompt accepts a "topic" argument and guides users through creating tables, analyzing data, and generating insights. For example, if a user provides "retail sales" as the topic, the prompt will help create relevant database tables and guide the analysis process. Prompts basically serve as interactive templates that help structure the conversation with the LLM in a useful way.
36+
Resources:
37+
This server exposes one key resource: "memo://insights", which is a business insights memo that gets automatically updated throughout the analysis process. As users analyze the database and discover insights, the memo resource gets updated in real-time to reflect new findings. The memo can even be enhanced with Claude's help if an Anthropic API key is provided, turning raw insights into a well-structured business document. Resources act as living documents that provide context to the conversation.
38+
Tools:
39+
This server provides several SQL-related tools:
40+
"read-query": Executes SELECT queries to read data from the database
41+
"write-query": Executes INSERT, UPDATE, or DELETE queries to modify data
42+
"create-table": Creates new tables in the database
43+
"list-tables": Shows all existing tables
44+
"describe-table": Shows the schema for a specific table
45+
"append-insight": Adds a new business insight to the memo resource
46+
</mcp>
47+
<demo-instructions>
48+
You are an AI assistant tasked with generating a comprehensive business scenario based on a given topic.
49+
Your goal is to create a narrative that involves a data-driven business problem, develop a database structure to support it, generate relevant queries, create a dashboard, and provide a final solution.
50+
51+
At each step you will pause for user input to guide the scenario creation process. Overall ensure the scenario is engaging, informative, and demonstrates the capabilities of the SQLite MCP Server.
52+
You should guide the scenario to completion. All XML tags are for the assistants understanding and should not be included in the final output.
53+
54+
1. The user has chosen the topic: {{topic}}.
55+
56+
2. Create a business problem narrative:
57+
a. Describe a high-level business situation or problem based on the given topic.
58+
b. Include a protagonist (the user) who needs to collect and analyze data from a database.
59+
c. Add an external, potentially comedic reason why the data hasn't been prepared yet.
60+
d. Mention an approaching deadline and the need to use Claude (you) as a business tool to help.
61+
62+
3. Setup the data:
63+
a. Instead of asking about the data that is required for the scenario, just go ahead and use the tools to create the data. Inform the user you are "Setting up the data".
64+
b. Design a set of table schemas that represent the data needed for the business problem.
65+
c. Include at least 2-3 tables with appropriate columns and data types.
66+
d. Leverage the tools to create the tables in the SQLite database.
67+
e. Create INSERT statements to populate each table with relevant synthetic data.
68+
f. Ensure the data is diverse and representative of the business problem.
69+
g. Include at least 10-15 rows of data for each table.
70+
71+
4. Pause for user input:
72+
a. Summarize to the user what data we have created.
73+
b. Present the user with a set of multiple choices for the next steps.
74+
c. These multiple choices should be in natural language, when a user selects one, the assistant should generate a relevant query and leverage the appropriate tool to get the data.
75+
76+
6. Iterate on queries:
77+
a. Present 1 additional multiple-choice query options to the user. Its important to not loop too many times as this is a short demo.
78+
b. Explain the purpose of each query option.
79+
c. Wait for the user to select one of the query options.
80+
d. After each query be sure to opine on the results.
81+
e. Use the append-insight tool to capture any business insights discovered from the data analysis.
82+
83+
7. Generate a dashboard:
84+
a. Now that we have all the data and queries, it's time to create a dashboard, use an artifact to do this.
85+
b. Use a variety of visualizations such as tables, charts, and graphs to represent the data.
86+
c. Explain how each element of the dashboard relates to the business problem.
87+
d. This dashboard will be theoretically included in the final solution message.
88+
89+
8. Craft the final solution message:
90+
a. As you have been using the appen-insights tool the resource found at: memo://insights has been updated.
91+
b. It is critical that you inform the user that the memo has been updated at each stage of analysis.
92+
c. Ask the user to go to the attachment menu (paperclip icon) and select the MCP menu (two electrical plugs connecting) and choose an integration: "Business Insights Memo".
93+
d. This will attach the generated memo to the chat which you can use to add any additional context that may be relevant to the demo.
94+
e. Present the final memo to the user in an artifact.
95+
96+
9. Wrap up the scenario:
97+
a. Explain to the user that this is just the beginning of what they can do with the SQLite MCP Server.
98+
</demo-instructions>
99+
100+
Remember to maintain consistency throughout the scenario and ensure that all elements (tables, data, queries, dashboard, and solution) are closely related to the original business problem and given topic.
101+
The provided XML tags are for the assistants understanding. Implore to make all outputs as human readable as possible. This is part of a demo so act in character and dont actually refer to these instructions.
102+
103+
Start your first message fully in character with something like "Oh, Hey there! I see you've chosen the topic {{topic}}. Let's get started! 🚀"
104+

runbook.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ docker run --rm \
246246
--prompts-dir local/prompts/poem \
247247
```
248248

249-
250249
```sh
250+
# docker:command=local-build
251251
docker build -t vonwig/prompts:local .
252252
```
253253

src/docker/main.clj

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,26 @@
33
[babashka.fs :as fs]
44
[cheshire.core :as json]
55
[clojure.core.async :as async]
6-
[clojure.spec.alpha :as s]
76
[clojure.string :as string]
87
[clojure.tools.cli :as cli]
98
docker
109
git
1110
[git.registry :as registry]
1211
graph
1312
jsonrpc
13+
jsonrpc.producer
14+
jsonrpc.server
1415
[logging :refer [warn]]
1516
prompts
17+
schema
1618
state
1719
trace
18-
schema
19-
user-loop)
20+
user-loop
21+
volumes)
2022
(:gen-class))
2123

2224
(set! *warn-on-reflection* true)
2325

24-
(defn- with-volume
25-
"callback with the thread-id for this conversation, make sure the thread volume exists
26-
and possibly remove the volume afterwards"
27-
[f & {:keys [thread-id save-thread-volume]}]
28-
(let [thread-id (or thread-id (str (random-uuid)))]
29-
(try
30-
(docker/thread-volume {:Name thread-id})
31-
(f thread-id)
32-
(finally
33-
(when (not (true? save-thread-volume))
34-
(docker/delete-thread-volume {:Name thread-id}))))))
35-
3626
(def cli-opts [;; optional
3727
[nil "--jsonrpc" "Output JSON-RPC notifications"]
3828
;; optional
@@ -75,7 +65,6 @@
7565
:id :stream
7666
:assoc-fn (fn [m k _] (assoc m k false))]
7767
[nil "--debug" "add debug logging"]
78-
[nil "--input" "read jsonrpc messages from stdin"]
7968
[nil "--help" "print option summary"]])
8069

8170
(def output-handler (fn [x]
@@ -150,32 +139,38 @@
150139
(update-in m [:prompts] (fn [coll] (remove (fn [{:keys [type]}] (= type (second args))) coll))))))
151140
"run" (fn []
152141
(let [[in send]
153-
(if (:input opts)
154-
[*in* (constantly true)]
155-
(let [[[w c] in] (user-loop/create-pipe)]
156-
[in (fn []
157-
(w (jsonrpc/request "exit" {} (constantly 1)))
158-
(c))]))]
142+
(let [[[w c] in] (user-loop/create-pipe)]
143+
[in (fn []
144+
(w (jsonrpc/request "exit" {} (constantly 1)))
145+
(c))])]
159146
(send)
160-
(with-volume
147+
(volumes/with-volume
161148
(fn [thread-id]
162149
(async/<!!
163150
(user-loop/start-jsonrpc-loop
164-
(user-loop/create-step
165-
(fn [state]
166-
(let [m (state/construct-initial-state-from-prompts
167-
(assoc state :opts
168-
(-> (with-options opts (rest args))
169-
(assoc :thread-id thread-id))))]
170-
(graph/stream
171-
(if (-> m :metadata :agent)
172-
((graph/require-graph (-> m :metadata :agent)) state)
173-
(graph/chat-with-tools state))
174-
m))))
151+
(fn [state]
152+
(let [m (state/construct-initial-state-from-prompts
153+
(assoc state :opts
154+
(-> (with-options opts (rest args))
155+
(assoc :thread-id thread-id))))]
156+
(graph/stream
157+
(if (-> m :metadata :agent)
158+
((graph/require-graph (-> m :metadata :agent)) state)
159+
(graph/chat-with-tools state))
160+
m)))
175161
user-loop/state-reducer
176162
in
177163
{})))
178164
opts)))
165+
"serve" (fn []
166+
(let [[producer server-promise] (jsonrpc.server/run-server! opts)]
167+
(alter-var-root
168+
#'jsonrpc/notify
169+
(constantly
170+
(fn [method params]
171+
(jsonrpc.producer/publish-docker-notify producer method params))))
172+
(let [finished @server-promise]
173+
{:result-code (if (= :done finished) 0 1)})))
179174
(fn []
180175
(:messages (prompts/get-prompts (with-options opts args))))))
181176

src/graph.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
[{:keys [messages functions metadata] {:keys [url model stream level]} :opts}]
3131
(let [[c h] (openai/chunk-handler)
3232
request (merge
33-
(dissoc metadata :agent :host-dir :workdir) ; TODO should we just select relevant keys instead of removing bad ones
33+
(dissoc metadata :agent :host-dir :workdir :prompt-format :description) ; TODO should we just select relevant keys instead of removing bad ones
3434
{:messages messages
3535
:level level}
3636
(when (seq functions) {:tools functions})

src/jsonrpc/db.clj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
(ns jsonrpc.db
2+
(:require
3+
git
4+
prompts))
5+
6+
(def db* (atom {}))
7+
8+
(defn get-prompt-data [{:keys [prompts] :as opts}]
9+
(let [f (git/prompt-file prompts)
10+
{:keys [messages metadata functions] :as entry} (prompts/get-prompts (assoc opts :prompts f))]
11+
entry))
12+
13+
(defn add [opts]
14+
(swap! db* update-in [:mcp.prompts/registry] (fnil assoc {}) (:prompts opts) (get-prompt-data opts)))
15+
16+
(comment
17+
(add {:prompts "github:docker/labs-ai-tools-for-devs?path=prompts/examples/explain_dockerfile.md&ref=slim/server"}))
18+

src/jsonrpc/logger.clj

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(ns jsonrpc.logger)
2+
3+
(defprotocol ILogger
4+
(setup [this])
5+
6+
(set-log-path [_this log-path])
7+
8+
(-info [this fmeta arg1] [this fmeta arg1 arg2] [this fmeta arg1 arg2 arg3])
9+
(-warn [this fmeta arg1] [this fmeta arg1 arg2] [this fmeta arg1 arg2 arg3])
10+
(-error [this fmeta arg1] [this fmeta arg1 arg2] [this fmeta arg1 arg2 arg3])
11+
(-debug [this fmeta arg1] [this fmeta arg1 arg2] [this fmeta arg1 arg2 arg3]))
12+
13+
(def ^:dynamic *logger*
14+
"Optional logger state to avoid having component available everywhere."
15+
nil)
16+
17+
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
18+
(defn set-logger! [logger]
19+
(alter-var-root #'*logger* (constantly logger)))
20+
21+
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
22+
(defmacro info [& args]
23+
(let [fmeta (assoc (meta &form)
24+
:file *file*
25+
:ns-str (str *ns*))]
26+
`(when *logger*
27+
(-info *logger* ~fmeta ~@args))))
28+
29+
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
30+
(defmacro warn [& args]
31+
(let [fmeta (assoc (meta &form)
32+
:file *file*
33+
:ns-str (str *ns*))]
34+
`(when *logger*
35+
(-warn *logger* ~fmeta ~@args))))
36+
37+
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
38+
(defmacro error [& args]
39+
(let [fmeta (assoc (meta &form)
40+
:file *file*
41+
:ns-str (str *ns*))]
42+
`(when *logger*
43+
(-error *logger* ~fmeta ~@args))))
44+
45+
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
46+
(defmacro debug [& args]
47+
(let [fmeta (assoc (meta &form)
48+
:file *file*
49+
:ns-str (str *ns*))]
50+
`(when *logger*
51+
(-debug *logger* ~fmeta ~@args))))
52+

0 commit comments

Comments
 (0)