Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5043fd9

Browse files
committed
Moved the demo prompt, resources and tools into their own project, to be used by all the transport examples.
1 parentec53b41 commit5043fd9

File tree

12 files changed

+252
-195
lines changed

12 files changed

+252
-195
lines changed

‎example/clj-server-sse/deps.edn‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{:paths ["src"]
22

3-
:deps {org.clojure/clojure {:mvn/version"1.12.1"}
4-
nrepl/nrepl {:mvn/version"1.3.1"}
5-
http-kit/http-kit {:mvn/version"2.9.0-alpha6"}
6-
com.taoensso/telemere {:mvn/version"1.0.1"}
7-
metosin/jsonista {:mvn/version"0.3.13"}
8-
metosin/reitit-ring {:mvn/version"0.9.1"}
9-
metosin/reitit-core {:mvn/version"0.9.1"}
10-
medley/medley {:mvn/version"1.4.0"}
11-
fi.metosin/mcp-toolkit {:local/root"../.."}}
3+
:deps {org.clojure/clojure {:mvn/version"1.12.1"}
4+
nrepl/nrepl {:mvn/version"1.3.1"}
5+
http-kit/http-kit {:mvn/version"2.9.0-alpha6"}
6+
com.taoensso/telemere {:mvn/version"1.0.1"}
7+
metosin/jsonista {:mvn/version"0.3.13"}
8+
metosin/reitit-ring {:mvn/version"0.9.1"}
9+
metosin/reitit-core {:mvn/version"0.9.1"}
10+
medley/medley {:mvn/version"1.4.0"}
11+
fi.metosin/mcp-toolkit {:local/root"../.."}
12+
example/common-mcp-content {:local/root"../common-mcp-content"}
13+
,}
1214

1315
:aliases {:dev {:extra-deps {}
1416
:extra-paths ["dev"]

‎example/clj-server-sse/src/example/my_server.clj‎

Lines changed: 8 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(nsexample.my-server
22
(:require
33
[clojure.string:as str]
4+
[example.server-content:as content]
45
[example.transport.sse:as sse]
56
[mcp-toolkit.server:as server]
67
[org.httpkit.server:as http-kit]
@@ -19,98 +20,21 @@
1920

2021
;; Example of usage of this library.
2122

22-
(deftalk-like-pirate-prompt
23-
{:name"pirate_mode_prompt"
24-
:description"Talk like a pirate prompt"
25-
:arguments [{:name"expressions"
26-
:description"Comma-separated expressions"
27-
:requiredfalse}]
28-
:complete-fn (fn [context name value]
29-
(when (= name"expressions")
30-
(when-not (str/includes? name"!")
31-
{:completion {:values [(str value"!")
32-
(str value"~#!")
33-
(str value"!#@!!")]
34-
:total3
35-
:hasMorefalse}})))
36-
:prompt-fn (fn [context {:keys [expressions]}]
37-
{:description (str"A talk-like-a-pirate prompt which includes the expressions:" expressions)
38-
:messages [{:role"user"
39-
:content {:type"text"
40-
:text (->> ["You are a sea pirate and you need to talk to the user in a tone that reassembles talk-like-a-pirate style."
41-
(when-not (str/blank? expressions)
42-
(str"You also need to randomly use the following expressions every few sentences:" expressions"."))
43-
(str"Start by introducing yourself in a spectacular way and talk about your pet parrot sitting on your shoulder."
44-
"Ask the user to choose a new and creative name for the parrot.")]
45-
(filter some?)
46-
(str/join""))}}]})})
47-
48-
(defhello-world-resource
49-
{:uri"file:///doc/hello.md"
50-
:name"hello.md"
51-
:description"Documentation's intro"
52-
:mimeType"text/markdown; charset=UTF-8"
53-
;;:blob ,,,
54-
:text"Hello, `world!`"})
55-
56-
(defmy-resource-templates
57-
[{:uriTemplate"file:///my-root-dir/{path}"
58-
:name"Project files"
59-
:description"Access files in the project directory"
60-
:mimeType"application/octet-stream"}])
61-
62-
(defnmy-resource-uri-complete-fn [context uri name value]
63-
(when (and (= uri"file:///my-root-dir/{path}")
64-
(= name"path"))
65-
(let [paths ["about""alpha""beta"]
66-
values (filterv (fn [path] (str/starts-with? path value)) paths)]
67-
{:completion {:values (take100 values)
68-
:total (count values)
69-
:hasMore (> (count values)100)}})))
70-
71-
(defparentify-tool
72-
{:name"parentify"
73-
:description"Parentify a text: wraps a text within parenthesis."
74-
:inputSchema {:type"object"
75-
:properties {:text {:type"string"
76-
:description"the text to be parentified"}}
77-
:required [:text]}
78-
:tool-fn (fn [context arguments]
79-
(-> (p/let [text (str"(" (:text arguments)")")
80-
_ (p/delay1000)
81-
_ (server/notify-progress context {:progress1
82-
:total3
83-
:message"thinking ..."})
84-
_ (p/delay1000)
85-
_ (server/notify-progress context {:progress2
86-
:total3
87-
:message"thinking harder ..."})
88-
_ (when @(:is-cancelled context)
89-
(throw (ex-info"tool was cancelled" {:note"too bad, was almost done"})))
90-
91-
_ (p/delay1000)]
92-
{:content [{:type"text"
93-
:text text}]
94-
:isErrorfalse})
95-
(p/catch (fn [exception]
96-
{:content [{:type"text"
97-
:text (str"Something went wrong:" (ex-message exception))}]
98-
:isErrortrue}))))})
99-
10023
(defncreate-session
101-
"Createsan mcp-toolkit server session atom.
24+
"Createsa mcp-toolkit server session atom.
10225
10326
Will be used to create a new session for each client connection.
10427
10528
Accepts the http server context and a session id.
10629
"
10730
[_context _session-id]
10831
(atom
109-
(server/create-session {:prompts [talk-like-pirate-prompt]
110-
:resources [hello-world-resource]
111-
:tools [parentify-tool]
112-
:resource-templates my-resource-templates
113-
:resource-uri-complete-fn my-resource-uri-complete-fn})))
32+
(server/create-session {:prompts [content/talk-like-pirate-prompt]
33+
:resources [content/hello-doc-resource
34+
content/world-doc-resource]
35+
:tools [content/parentify-tool]
36+
:resource-templates content/my-resource-templates
37+
:resource-uri-complete-fn content/my-resource-uri-complete-fn})))
11438

11539
(defdefault-transport-env
11640
{:dev?true

‎example/clj-server-sse/src/example/transport/sse.clj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(nsexample.transport.sse
2-
"Thisnamespaces provides a 2024-11-05 compatible SSE transport for MCP Toolkit."
2+
"Thisnamespace provides a 2024-11-05 compatible SSE transport for MCP Toolkit."
33
(:require
44
[clojure.string:as str]
55
[jsonista.core:as j]

‎example/cljc-client-stdio/deps.edn‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
thheller/shadow-cljs {:mvn/version"3.1.7"}
66
metosin/jsonista {:mvn/version"0.3.13"}
77
fi.metosin/mcp-toolkit {:local/root"../.."}
8+
example/common-mcp-content {:local/root"../common-mcp-content"}
89
,}
910

1011
:aliases {;; clojure -M:mcp-client

‎example/cljc-client-stdio/src/example/my_client.cljc‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[mcp-toolkit.client:as client]
44
[mcp-toolkit.json-rpc:as json-rpc]
55
[promesa.core:as p]
6+
[example.client-content:as content]
67
#?(:clj [jsonista.core:as j])
78
#?(:cljs ["child_process":refer [spawn]])
89
#?(:cljs ["path":as path]))
@@ -19,15 +20,8 @@
1920
(atom
2021
(client/create-session {:client-capabilities {:roots {:listChangedtrue}
2122
:sampling {}}
22-
:roots [[{:uri"file:///home/user/projects/my-root"
23-
:name"My project root"}]]
24-
:on-sampling-requested (fn [context]
25-
(let [{:keys [session message]} context]
26-
{:role"assistant"
27-
:content {:type"text"
28-
:text"You are absolutely right, and the answer is 42."}
29-
:model"The Hitchhiker's Guide to the Galaxy"
30-
:stopReason"endTurn"}))})))
23+
:roots content/roots
24+
:on-sampling-requested content/sampling-handler})))
3125

3226
(defcontext
3327
(atom {:session session}))

‎example/cljc-server-stdio/deps.edn‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
nrepl/nrepl {:mvn/version"1.3.1"}
77
metosin/jsonista {:mvn/version"0.3.13"}
88
fi.metosin/mcp-toolkit {:local/root"../.."}
9+
example/common-mcp-content {:local/root"../common-mcp-content"}
910
,}
1011

1112
:aliases {;; clojure -X:mcp-server

‎example/cljc-server-stdio/src/example/my_server.cljc‎

Lines changed: 7 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,111 +3,26 @@
33
[mcp-toolkit.server:as server]
44
[mcp-toolkit.json-rpc:as json-rpc]
55
[promesa.core:as p]
6+
[example.server-content:as content]
67
#?(:clj [jsonista.core:as j])
78
#?(:clj [nrepl.server:as nrepl]))
89
#?(:clj (:import (clojure.lang LineNumberingPushbackReader)
910
(java.io OutputStreamWriter))))
1011

1112
;; Example of usage of this library.
1213

13-
(deftalk-like-pirate-prompt
14-
{:name"pirate_mode_prompt"
15-
:description"Talk like a pirate prompt"
16-
:arguments [{:name"expressions"
17-
:description"Comma-separated expressions"
18-
:requiredfalse}]
19-
:complete-fn (fn [context name value]
20-
(when (= name"expressions")
21-
(when-not (str/includes? name"!")
22-
{:completion {:values [(str value"!")
23-
(str value"~#!")
24-
(str value"!#@!!")]
25-
:total3
26-
:hasMorefalse}})))
27-
:prompt-fn (fn [context {:keys [expressions]}]
28-
{:description (str"A talk-like-a-pirate prompt which includes the expressions:" expressions)
29-
:messages [{:role"user"
30-
:content {:type"text"
31-
:text (->> ["You are a sea pirate and you need to talk to the user in a tone that reassembles talk-like-a-pirate style."
32-
(when-not (str/blank? expressions)
33-
(str"You also need to randomly use the following expressions every few sentences:" expressions"."))
34-
(str"Start by introducing yourself in a spectacular way and talk about your pet parrot sitting on your shoulder."
35-
"Ask the user to choose a new and creative name for the parrot.")]
36-
(filter some?)
37-
(str/join""))}}]})})
38-
39-
(defhello-doc-resource
40-
{:uri"file:///doc/hello.md"
41-
:name"hello.md"
42-
:description"First part of the\"hello world\" resources"
43-
:mimeType"text/markdown; charset=UTF-8"
44-
;;:blob ,,,
45-
:text"Hello"})
46-
47-
(defworld-doc-resource
48-
{:uri"file:///doc/world.md"
49-
:name"world.md"
50-
:description"Second part of the\"hello world\" resources"
51-
:mimeType"text/markdown; charset=UTF-8"
52-
;;:blob ,,,
53-
:text"world!"})
54-
55-
(defmy-resource-templates
56-
[{:uriTemplate"file:///doc/{path}"
57-
:name"Documentation files"
58-
:description"The documentation files"
59-
:mimeType"text/markdown; charset=UTF-8"}])
60-
61-
(defnmy-resource-uri-complete-fn [context uri name value]
62-
(when (and (= uri"file:///doc/{path}")
63-
(= name"path"))
64-
(let [paths ["hello.md""world.md"]
65-
values (filterv (fn [path] (str/starts-with? path value)) paths)]
66-
{:completion {:values (take100 values)
67-
:total (count values)
68-
:hasMore (> (count values)100)}})))
69-
70-
(defparentify-tool
71-
{:name"parentify"
72-
:description"Parentify a text: wraps a text within parenthesis."
73-
:inputSchema {:type"object"
74-
:properties {:text {:type"string"
75-
:description"the text to be parentified"}}
76-
:required [:text]}
77-
:tool-fn (fn [context arguments]
78-
(-> (p/let [text (str"(" (:text arguments)")")
79-
_ (p/delay1000)
80-
_ (server/notify-progress context {:progress1
81-
:total3
82-
:message"thinking ..."})
83-
_ (p/delay1000)
84-
_ (server/notify-progress context {:progress2
85-
:total3
86-
:message"thinking harder ..."})
87-
_ (when @(:is-cancelled context)
88-
(throw (ex-info"tool was cancelled" {:note"too bad, was almost done"})))
89-
90-
_ (p/delay1000)]
91-
{:content [{:type"text"
92-
:text text}]
93-
:isErrorfalse})
94-
(p/catch (fn [exception]
95-
{:content [{:type"text"
96-
:text (str"Something went wrong:" (ex-message exception))}]
97-
:isErrortrue}))))})
98-
9914
;;
10015
;; State
10116
;;
10217

10318
(defsession
10419
(atom
105-
(server/create-session {:prompts [talk-like-pirate-prompt]
106-
:resources [hello-doc-resource
107-
world-doc-resource]
108-
:tools [parentify-tool]
109-
:resource-templates my-resource-templates
110-
:resource-uri-complete-fn my-resource-uri-complete-fn})))
20+
(server/create-session {:prompts [content/talk-like-pirate-prompt]
21+
:resources [content/hello-doc-resource
22+
content/world-doc-resource]
23+
:tools [content/parentify-tool]
24+
:resource-templatescontent/my-resource-templates
25+
:resource-uri-complete-fncontent/my-resource-uri-complete-fn})))
11126

11227
;;
11328
;; Platform-specific threading, transport & I/O stuffs
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
*.iml
3+
.cpcache
4+
.nrepl-port

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp