Skip to content

Commit

Permalink
mcp-sqlite as prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
slimslenderslacks committed Jan 8, 2025
1 parent 42748b9 commit ed0305d
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 18 deletions.
1 change: 1 addition & 0 deletions prompts/examples/curl.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
model: claude-3-5-sonnet-20241022
tools:
- name: curl
---
Expand Down
1 change: 1 addition & 0 deletions prompts/examples/explain_dockerfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ description: |
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.
model: claude-3-5-sonnet-20241022
tools:
- name: cat_file
description: fetch a file
Expand Down
1 change: 1 addition & 0 deletions prompts/examples/hello_world.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
name: hello-docker
description: run the hello-docker
model: claude-3-5-sonnet-20241022
tools:
- name: hello-docker
description: print a secret message
Expand Down
52 changes: 49 additions & 3 deletions prompts/examples/mcp-sqlite.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,70 @@
---
description: |
A prompt to seed the database with initial data and demonstrate what you can do with an SQLite MCP Server + Claude
model: claude-3-5-sonnet-20241022
tools:
- name: read-query
description: Execute a SELECT query on the SQLite database
container:
parameters: &query
type: object
properties:
query:
type: string
description: SELECT SQL query to execute
container: &sqlite
image: vonwig/sqlite:latest
command:
- "{{database}}"
- "{{sql}}"
- "/mcp/test1.db"
- "{{query|safe}}"
mounts: &mounts
- "mcp-test:/mcp"
- name: write-query
description: Execute an INSERT, UPDATE, or DELETE query on the SQLite database
parameters: *query
container: *sqlite
- name: create-table
description: Create a new table in the SQLite database
parameters: *query
container: *sqlite
- name: list-tables
description: List all tables in the SQLite database
container:
image: vonwig/sqlite:latest
command:
- "/mcp/test1.db"
- "SELECT name from sqlite_master WHERE type='table'"
mounts: *mounts
- name: describe-table
description: Get the schema information for a specific table
parameters:
type: object
properties:
table_name:
type: string
description: Name of the table to describe
container:
image: vonwig/sqlite:latest
command:
- "/mcp/test1.db"
- "PRAGMA table_info({{table_name}})"
mounts: *mounts
- name: append-insight
description: Add a business insight to the memo
parameters:
type: object
properties:
insight:
type: string
description: Business insight discovered from data analysis
container:
image: vonwig/bash_alpine
command:
- "-c"
- "echo '{{insight|safe}}' >> /mcp/insights.txt"
mounts: *mounts
prompt-format: django
parameter-values:
topic: Ocean Conservation
---

# prompt user
Expand Down
4 changes: 2 additions & 2 deletions src/claude.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(try
(string/trim (slurp (io/file (or (System/getenv "CLAUDE_API_KEY_LOCATION") (System/getenv "HOME")) ".claude-api-key")))
(catch Throwable _
(throw (ex-info "Unable to read claude-api-key secret" {})))))
(throw (ex-info "Unable to read claude api-key secret" {})))))

(defn parse-sse [s]
(when (string/starts-with? s "data:")
Expand Down Expand Up @@ -52,7 +52,7 @@
(if tool_calls
{:role (:role message)
:content (concat
(when (:content message) [{:type "text" :text (:content message)}])
(when (and (:content message) (not (= "" (:content message)))) [{:type "text" :text (:content message)}])
(->> tool_calls
(map (fn [{:keys [id function]}]
{:type "tool_use"
Expand Down
2 changes: 1 addition & 1 deletion src/graph.clj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
(let [[chunk-handler sample] (providers (llm-provider (or (:model metadata) model)))
[c h] (chunk-handler)
request (merge
(dissoc metadata :agent :host-dir :workdir :prompt-format :description :name) ; TODO should we just select relevant keys instead of removing bad ones
(dissoc metadata :agent :host-dir :workdir :prompt-format :description :name :parameter-values) ; TODO should we just select relevant keys instead of removing bad ones
{:messages messages
:level level}
(when (seq functions) {:tools functions})
Expand Down
25 changes: 14 additions & 11 deletions src/prompts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@
(fn [content]
(if prompts-file
(stache/render-string
content
m
{:partials (partials/file-partials
[(if (fs/directory? prompts-file) prompts-file (fs/parent prompts-file))]
".md")})
content
m
{:partials (partials/file-partials
[(if (fs/directory? prompts-file) prompts-file (fs/parent prompts-file))]
".md")})
(stache/render-string content m)))))

(defn selmer-render [m message]
Expand All @@ -145,9 +145,9 @@
(fn [content]
(selmer/render content m))))

(selmer/add-tag! :tip (fn [args context-map]
(format
"At the very end of the response, add this sentence: \"ℹ️ You can also ask: '%s'\", in the language used by the user, with the question in italic." (first args))))
(selmer/add-tag! :tip (fn [args context-map]
(format
"At the very end of the response, add this sentence: \"ℹ️ You can also ask: '%s'\", in the language used by the user, with the question in italic." (first args))))

(comment
(stache/render-string "yo {{a.0.content}}" {:a [{:content "blah"}]}))
Expand All @@ -159,7 +159,7 @@
returns map of messages, functions, metadata and optionally error"
[{:keys [parameters prompts user platform host-dir prompt-content] :as opts}]
(let [{:keys [metadata] :as prompt-data}
(cond
(cond
;; prompt content is already in opts
prompt-content
(markdown-parser/parse-prompts prompt-content)
Expand All @@ -179,8 +179,11 @@
:else
(markdown-parser/parse-prompts (slurp prompts)))

m (merge (run-extractors (:extractors metadata) opts) parameters)
renderer (if (= "django" (:prompt-format metadata))
m (merge
(run-extractors (:extractors metadata) opts)
parameters
(-> metadata :parameter-values))
renderer (if (= "django" (:prompt-format metadata))
(partial selmer-render (facts m user platform host-dir))
(partial moustache-render prompts (facts m user platform host-dir)))]
((schema/validate :schema/prompts-file)
Expand Down
4 changes: 3 additions & 1 deletion src/tools.clj
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@
false
(not= 0 exit-code))]
(cond
(and (= :exited done) (not exit-code-fail?))
(and (= :exited done) (not exit-code-fail?) pty-output (not (= "" pty-output)))
(resolve pty-output)
(and (= :exited done) (not exit-code-fail?))
(resolve "success")
(and (= :exited done) exit-code-fail?)
(fail (format "call exited with non-zero code (%d): %s" exit-code pty-output))
(= :timeout done)
Expand Down

0 comments on commit ed0305d

Please sign in to comment.