@@ -2,9 +2,11 @@ package main
22
33import (
44 "context"
5+ "encoding/base64"
56 "flag"
67 "fmt"
78 "log"
9+ "strconv"
810 "time"
911
1012 "github.com/mark3labs/mcp-go/mcp"
@@ -64,6 +66,7 @@ func NewMCPServer() *server.MCPServer {
6466 "1.0.0" ,
6567 server .WithResourceCapabilities (true , true ),
6668 server .WithPromptCapabilities (true ),
69+ server .WithToolCapabilities (true ),
6770 server .WithLogging (),
6871 server .WithHooks (hooks ),
6972 )
@@ -79,6 +82,12 @@ func NewMCPServer() *server.MCPServer {
7982 ),
8083 handleResourceTemplate ,
8184 )
85+
86+ resources := generateResources ()
87+ for _ , resource := range resources {
88+ mcpServer .AddResource (resource , handleGeneratedResource )
89+ }
90+
8291 mcpServer .AddPrompt (mcp .NewPrompt (string (SIMPLE ),
8392 mcp .WithPromptDescription ("A simple prompt" ),
8493 ), handleSimplePrompt )
@@ -180,27 +189,6 @@ func generateResources() []mcp.Resource {
180189 return resources
181190}
182191
183- func runUpdateInterval () {
184- // for range s.updateTicker.C {
185- // for uri := range s.subscriptions {
186- // s.server.HandleMessage(
187- // context.Background(),
188- // mcp.JSONRPCNotification{
189- // JSONRPC: mcp.JSONRPC_VERSION,
190- // Notification: mcp.Notification{
191- // Method: "resources/updated",
192- // Params: struct {
193- // Meta map[string]any `json:"_meta,omitempty"`
194- // }{
195- // Meta: map[string]any{"uri": uri},
196- // },
197- // },
198- // },
199- // )
200- // }
201- // }
202- }
203-
204192func handleReadResource (
205193 ctx context.Context ,
206194 request mcp.ReadResourceRequest ,
@@ -227,6 +215,43 @@ func handleResourceTemplate(
227215 }, nil
228216}
229217
218+ func handleGeneratedResource (
219+ ctx context.Context ,
220+ request mcp.ReadResourceRequest ,
221+ ) ([]mcp.ResourceContents , error ) {
222+ uri := request .Params .URI
223+
224+ var resourceNumber string
225+ if _ , err := fmt .Sscanf (uri , "test://static/resource/%s" , & resourceNumber ); err != nil {
226+ return nil , fmt .Errorf ("invalid resource URI format: %w" , err )
227+ }
228+
229+ num , err := strconv .Atoi (resourceNumber )
230+ if err != nil {
231+ return nil , fmt .Errorf ("invalid resource number: %w" , err )
232+ }
233+
234+ index := num - 1
235+
236+ if index % 2 == 0 {
237+ return []mcp.ResourceContents {
238+ mcp.TextResourceContents {
239+ URI : uri ,
240+ MIMEType : "text/plain" ,
241+ Text : fmt .Sprintf ("Text content for resource %d" , num ),
242+ },
243+ }, nil
244+ } else {
245+ return []mcp.ResourceContents {
246+ mcp.BlobResourceContents {
247+ URI : uri ,
248+ MIMEType : "application/octet-stream" ,
249+ Blob : base64 .StdEncoding .EncodeToString ([]byte (fmt .Sprintf ("Binary content for resource %d" , num ))),
250+ },
251+ }, nil
252+ }
253+ }
254+
230255func handleSimplePrompt (
231256 ctx context.Context ,
232257 request mcp.GetPromptRequest ,
0 commit comments