From 373bfa652da8e831d044cb595b18e3734c59494c Mon Sep 17 00:00:00 2001 From: Timur Sultanaev Date: Fri, 13 Dec 2024 21:13:39 +0100 Subject: [PATCH] snippets for hello world resource --- examples/hello_world_resource/main.go | 76 +++++++++++++++------------ 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/examples/hello_world_resource/main.go b/examples/hello_world_resource/main.go index 0bf158b..72e7d8c 100644 --- a/examples/hello_world_resource/main.go +++ b/examples/hello_world_resource/main.go @@ -12,44 +12,50 @@ import ( "go.uber.org/zap" ) -func main() { - // This example defines resource tool for MCP server - // , run it with: - // npx @modelcontextprotocol/inspector go run main.go - // , then in browser open http://localhost:5173 - // , then click Connect - // , then click List Resources - // , then click hello-world +// This example defines resource tool for MCP server +// , run it with: +// npx @modelcontextprotocol/inspector go run main.go +// , then in browser open http://localhost:5173 +// , then click Connect +// , then click List Resources +// , then click hello-world + +// --8<-- [start:tool] +func NewGreatResource() fxctx.Resource { + return fxctx.NewResource( + mcp.Resource{ + Name: "hello-world", + Uri: "hello-world://hello-world", + MimeType: Ptr("application/json"), + Description: Ptr("Hello World Resource"), + Annotations: &mcp.ResourceAnnotations{ + Audience: []mcp.Role{ + mcp.RoleAssistant, mcp.RoleUser, + }, + }, + }, + func(uri string) (*mcp.ReadResourceResult, error) { + return &mcp.ReadResourceResult{ + Contents: []interface{}{ + mcp.TextResourceContents{ + MimeType: Ptr("application/json"), + Text: `{"hello": "world"}`, + Uri: uri, + }, + }, + }, nil + }, + ) +} +// --8<-- [end:tool] + +// --8<-- [start:server] +func main() { err := app. NewFoxyApp(). // adding the resource to the app - WithResource(func() fxctx.Resource { - return fxctx.NewResource( - mcp.Resource{ - Name: "hello-world", - Uri: "hello-world://hello-world", - MimeType: Ptr("application/json"), - Description: Ptr("Hello World Resource"), - Annotations: &mcp.ResourceAnnotations{ - Audience: []mcp.Role{ - mcp.RoleAssistant, mcp.RoleUser, - }, - }, - }, - func(uri string) (*mcp.ReadResourceResult, error) { - return &mcp.ReadResourceResult{ - Contents: []interface{}{ - mcp.TextResourceContents{ - MimeType: Ptr("application/json"), - Text: `{"hello": "world"}`, - Uri: uri, - }, - }, - }, nil - }, - ) - }). + WithResource(NewGreatResource). // setting up server WithName("my-mcp-server"). WithVersion("0.0.1"). @@ -74,6 +80,8 @@ func main() { } } +// --8<-- [end:server] + func Ptr[T any](v T) *T { return &v }