Skip to content

Commit

Permalink
Slim/server (#33)
Browse files Browse the repository at this point in the history
Add server mode
  • Loading branch information
slimslenderslacks authored Dec 4, 2024
1 parent efe8567 commit 4a3504c
Show file tree
Hide file tree
Showing 19 changed files with 1,019 additions and 326 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
/tools_vector_store/.venv/
/tools_vector_store/tools.db
/tools_vector_store/chroma_db/
/docker-mcp-server.out
597 changes: 332 additions & 265 deletions deps-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
selmer/selmer {:mvn/version "1.12.61"}
cheshire/cheshire {:mvn/version "5.13.0"}
org.clojure/core.async {:mvn/version "1.6.681"}
org.babashka/http-client {:mvn/version "0.4.12"}}
org.babashka/http-client {:mvn/version "0.4.12"}
com.taoensso/timbre {:mvn/version "5.2.1"}
io.github.slimslenderslacks/lsp4clj {:git/sha "995abcdbfebfa7ef6550625fe1ef3ca2c7683292"}
funcool/promesa {:mvn/version "9.0.470"}}
:aliases {:main {:main-opts ["-m" "docker.main"]}
:build {:ns-default build
:deps {io.github.clojure/tools.build {:git/tag "v0.9.5" :git/sha "24f2894"}}}}}
66 changes: 66 additions & 0 deletions prompts/examples/explain_dockerfile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
description: |
Provide a detailed description, analysis, or annotation of a given Dockerfile,
explaining its structure and functionality.
Synonyms: explain my Dockerfile, annotate this Dockerfile...
This tool can explain a pre-provided Dockerfile but it can also fetch the Dockerfile from the user's workspace.
tools:
- name: cat_file
description: fetch a file
parameters:
type: object
properties:
path:
type: string
description: Path of the folder to delete
container:
image: vonwig/bash_alpine
command:
- "cat {{path|safe}}"
- name: findutils-by-name
description: find files in a project by name
parameters:
type: object
properties:
glob:
type: string
description: the glob pattern for files that should be found
container:
image: vonwig/findutils:latest
command:
- find
- .
- -name
- "{{glob|safe}}"
prompt-format: "django"
---

# prompt user

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.

After fetching the Dockerfile contents, explain the Dockerfile line by line.

Requirements:
+ Each command MUST pre prefixed with its line number.
+ For each command, add links to the documentation if necessary.
+ Use a new paragraph for links to the documentation.
+ {% tip "How can I optimize my Dockerfile?" %}
+ {% tip "Can you rate my Dockerfile?" %}

Sample final output:

**01: FROM golang**

directly explain what it does, without prefix...

[See documentation](https://docs.docker.com/engine/reference/builder/#command)

**05: CMD something**

directly explain what it does, without prefix...

[See documentation](https://docs.docker.com/engine/reference/builder/#command)

104 changes: 104 additions & 0 deletions prompts/examples/mcp-sqlite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
description: |
A prompt to seed the database with initial data and demonstrate what you can do with an SQLite MCP Server + Claude
tools:
- name: read-query
description: Execute a SELECT query on the SQLite database
container:
image: vonwig/sqlite:latest
command:
- "{{database}}"
- "{{sql}}"
- name: write-query
description: Execute an INSERT, UPDATE, or DELETE query on the SQLite database
- name: create-table
description: Create a new table in the SQLite database
- name: list-tables
description: List all tables in the SQLite database
- name: describe-table
description: Get the schema information for a specific table
- name: append-insight
description: Add a business insight to the memo
---

# prompt user

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.
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.
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.
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'.
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.
They have already used a prompt and provided a topic. The topic is: {{topic}}. The user is now ready to begin the demo.
Here is some more information about mcp and this specific mcp server:
<mcp>
Prompts:
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.
Resources:
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.
Tools:
This server provides several SQL-related tools:
"read-query": Executes SELECT queries to read data from the database
"write-query": Executes INSERT, UPDATE, or DELETE queries to modify data
"create-table": Creates new tables in the database
"list-tables": Shows all existing tables
"describe-table": Shows the schema for a specific table
"append-insight": Adds a new business insight to the memo resource
</mcp>
<demo-instructions>
You are an AI assistant tasked with generating a comprehensive business scenario based on a given topic.
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.

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.
You should guide the scenario to completion. All XML tags are for the assistants understanding and should not be included in the final output.

1. The user has chosen the topic: {{topic}}.

2. Create a business problem narrative:
a. Describe a high-level business situation or problem based on the given topic.
b. Include a protagonist (the user) who needs to collect and analyze data from a database.
c. Add an external, potentially comedic reason why the data hasn't been prepared yet.
d. Mention an approaching deadline and the need to use Claude (you) as a business tool to help.

3. Setup the data:
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".
b. Design a set of table schemas that represent the data needed for the business problem.
c. Include at least 2-3 tables with appropriate columns and data types.
d. Leverage the tools to create the tables in the SQLite database.
e. Create INSERT statements to populate each table with relevant synthetic data.
f. Ensure the data is diverse and representative of the business problem.
g. Include at least 10-15 rows of data for each table.

4. Pause for user input:
a. Summarize to the user what data we have created.
b. Present the user with a set of multiple choices for the next steps.
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.

6. Iterate on queries:
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.
b. Explain the purpose of each query option.
c. Wait for the user to select one of the query options.
d. After each query be sure to opine on the results.
e. Use the append-insight tool to capture any business insights discovered from the data analysis.

7. Generate a dashboard:
a. Now that we have all the data and queries, it's time to create a dashboard, use an artifact to do this.
b. Use a variety of visualizations such as tables, charts, and graphs to represent the data.
c. Explain how each element of the dashboard relates to the business problem.
d. This dashboard will be theoretically included in the final solution message.

8. Craft the final solution message:
a. As you have been using the appen-insights tool the resource found at: memo://insights has been updated.
b. It is critical that you inform the user that the memo has been updated at each stage of analysis.
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".
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.
e. Present the final memo to the user in an artifact.

9. Wrap up the scenario:
a. Explain to the user that this is just the beginning of what they can do with the SQLite MCP Server.
</demo-instructions>

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.
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.

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! 🚀"

2 changes: 1 addition & 1 deletion runbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ docker run --rm \
--prompts-dir local/prompts/poem \
```


```sh
# docker:command=local-build
docker build -t vonwig/prompts:local .
```

Expand Down
63 changes: 29 additions & 34 deletions src/docker/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,26 @@
[babashka.fs :as fs]
[cheshire.core :as json]
[clojure.core.async :as async]
[clojure.spec.alpha :as s]
[clojure.string :as string]
[clojure.tools.cli :as cli]
docker
git
[git.registry :as registry]
graph
jsonrpc
jsonrpc.producer
jsonrpc.server
[logging :refer [warn]]
prompts
schema
state
trace
schema
user-loop)
user-loop
volumes)
(:gen-class))

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

(defn- with-volume
"callback with the thread-id for this conversation, make sure the thread volume exists
and possibly remove the volume afterwards"
[f & {:keys [thread-id save-thread-volume]}]
(let [thread-id (or thread-id (str (random-uuid)))]
(try
(docker/thread-volume {:Name thread-id})
(f thread-id)
(finally
(when (not (true? save-thread-volume))
(docker/delete-thread-volume {:Name thread-id}))))))

(def cli-opts [;; optional
[nil "--jsonrpc" "Output JSON-RPC notifications"]
;; optional
Expand Down Expand Up @@ -75,7 +65,6 @@
:id :stream
:assoc-fn (fn [m k _] (assoc m k false))]
[nil "--debug" "add debug logging"]
[nil "--input" "read jsonrpc messages from stdin"]
[nil "--help" "print option summary"]])

(def output-handler (fn [x]
Expand Down Expand Up @@ -150,32 +139,38 @@
(update-in m [:prompts] (fn [coll] (remove (fn [{:keys [type]}] (= type (second args))) coll))))))
"run" (fn []
(let [[in send]
(if (:input opts)
[*in* (constantly true)]
(let [[[w c] in] (user-loop/create-pipe)]
[in (fn []
(w (jsonrpc/request "exit" {} (constantly 1)))
(c))]))]
(let [[[w c] in] (user-loop/create-pipe)]
[in (fn []
(w (jsonrpc/request "exit" {} (constantly 1)))
(c))])]
(send)
(with-volume
(volumes/with-volume
(fn [thread-id]
(async/<!!
(user-loop/start-jsonrpc-loop
(user-loop/create-step
(fn [state]
(let [m (state/construct-initial-state-from-prompts
(assoc state :opts
(-> (with-options opts (rest args))
(assoc :thread-id thread-id))))]
(graph/stream
(if (-> m :metadata :agent)
((graph/require-graph (-> m :metadata :agent)) state)
(graph/chat-with-tools state))
m))))
(fn [state]
(let [m (state/construct-initial-state-from-prompts
(assoc state :opts
(-> (with-options opts (rest args))
(assoc :thread-id thread-id))))]
(graph/stream
(if (-> m :metadata :agent)
((graph/require-graph (-> m :metadata :agent)) state)
(graph/chat-with-tools state))
m)))
user-loop/state-reducer
in
{})))
opts)))
"serve" (fn []
(let [[producer server-promise] (jsonrpc.server/run-server! opts)]
(alter-var-root
#'jsonrpc/notify
(constantly
(fn [method params]
(jsonrpc.producer/publish-docker-notify producer method params))))
(let [finished @server-promise]
{:result-code (if (= :done finished) 0 1)})))
(fn []
(:messages (prompts/get-prompts (with-options opts args))))))

Expand Down
2 changes: 1 addition & 1 deletion src/graph.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
[{:keys [messages functions metadata] {:keys [url model stream level]} :opts}]
(let [[c h] (openai/chunk-handler)
request (merge
(dissoc metadata :agent :host-dir :workdir) ; TODO should we just select relevant keys instead of removing bad ones
(dissoc metadata :agent :host-dir :workdir :prompt-format :description) ; TODO should we just select relevant keys instead of removing bad ones
{:messages messages
:level level}
(when (seq functions) {:tools functions})
Expand Down
18 changes: 18 additions & 0 deletions src/jsonrpc/db.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns jsonrpc.db
(:require
git
prompts))

(def db* (atom {}))

(defn get-prompt-data [{:keys [prompts] :as opts}]
(let [f (git/prompt-file prompts)
{:keys [messages metadata functions] :as entry} (prompts/get-prompts (assoc opts :prompts f))]
entry))

(defn add [opts]
(swap! db* update-in [:mcp.prompts/registry] (fnil assoc {}) (:prompts opts) (get-prompt-data opts)))

(comment
(add {:prompts "github:docker/labs-ai-tools-for-devs?path=prompts/examples/explain_dockerfile.md&ref=slim/server"}))

52 changes: 52 additions & 0 deletions src/jsonrpc/logger.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
(ns jsonrpc.logger)

(defprotocol ILogger
(setup [this])

(set-log-path [_this log-path])

(-info [this fmeta arg1] [this fmeta arg1 arg2] [this fmeta arg1 arg2 arg3])
(-warn [this fmeta arg1] [this fmeta arg1 arg2] [this fmeta arg1 arg2 arg3])
(-error [this fmeta arg1] [this fmeta arg1 arg2] [this fmeta arg1 arg2 arg3])
(-debug [this fmeta arg1] [this fmeta arg1 arg2] [this fmeta arg1 arg2 arg3]))

(def ^:dynamic *logger*
"Optional logger state to avoid having component available everywhere."
nil)

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn set-logger! [logger]
(alter-var-root #'*logger* (constantly logger)))

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defmacro info [& args]
(let [fmeta (assoc (meta &form)
:file *file*
:ns-str (str *ns*))]
`(when *logger*
(-info *logger* ~fmeta ~@args))))

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defmacro warn [& args]
(let [fmeta (assoc (meta &form)
:file *file*
:ns-str (str *ns*))]
`(when *logger*
(-warn *logger* ~fmeta ~@args))))

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defmacro error [& args]
(let [fmeta (assoc (meta &form)
:file *file*
:ns-str (str *ns*))]
`(when *logger*
(-error *logger* ~fmeta ~@args))))

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defmacro debug [& args]
(let [fmeta (assoc (meta &form)
:file *file*
:ns-str (str *ns*))]
`(when *logger*
(-debug *logger* ~fmeta ~@args))))

12 changes: 12 additions & 0 deletions src/jsonrpc/producer.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns jsonrpc.producer)

(defprotocol IProducer
(publish-exit [this params])
(publish-progress [this params])
(publish-log [this params])
(publish-prompt-list-changed [this params])
(publish-resource-list-changed [this params])
(publish-resource-updated [this params])
(publish-tool-list-changed [this params])
(publish-docker-notify [this method params]))

Loading

0 comments on commit 4a3504c

Please sign in to comment.