Skip to content

Commit

Permalink
snippets for hello world resource
Browse files Browse the repository at this point in the history
  • Loading branch information
strowk committed Dec 13, 2024
1 parent 23dac45 commit 373bfa6
Showing 1 changed file with 42 additions and 34 deletions.
76 changes: 42 additions & 34 deletions examples/hello_world_resource/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand All @@ -74,6 +80,8 @@ func main() {
}
}

// --8<-- [end:server]

func Ptr[T any](v T) *T {
return &v
}

0 comments on commit 373bfa6

Please sign in to comment.