Skip to content

Commit 63c687a

Browse files
committed
Update example and README
1 parent 3a777e4 commit 63c687a

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

README.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ func main() {
4646
}
4747
}
4848

49-
func helloHandler(ctx context.Context, arguments map[string]interface{}) (*mcp.CallToolResult, error) {
50-
name, ok := arguments["name"].(string)
49+
func helloHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
50+
name, ok := request.Params.Arguments["name"].(string)
5151
if !ok {
5252
return mcp.NewToolResultError("name must be a string"), nil
5353
}
@@ -137,10 +137,10 @@ func main() {
137137
)
138138

139139
// Add the calculator handler
140-
s.AddTool(calculatorTool, func(args map[string]interface{}) (*mcp.CallToolResult, error) {
141-
op := args["operation"].(string)
142-
x := args["x"].(float64)
143-
y := args["y"].(float64)
140+
s.AddTool(calculatorTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
141+
op := request.Params.Arguments["operation"].(string)
142+
x := request.Params.Arguments["x"].(float64)
143+
y := request.Params.Arguments["y"].(float64)
144144

145145
var result float64
146146
switch op {
@@ -223,7 +223,7 @@ resource := mcp.NewResource(
223223
)
224224

225225
// Add resource with its handler
226-
s.AddResource(resource, func(ctx context.Context) ([]interface{}, error) {
226+
s.AddResource(resource, func(ctx context.Context, request mcp.ReadResourceRequest) ([]interface{}, error) {
227227
content, err := os.ReadFile("README.md")
228228
if err != nil {
229229
return nil, err
@@ -254,8 +254,8 @@ template := mcp.NewResourceTemplate(
254254
)
255255

256256
// Add template with its handler
257-
s.AddResourceTemplate(template, func(ctx context.Context, args map[string]interface{}) ([]interface{}, error) {
258-
userID := args["id"].(string)
257+
s.AddResourceTemplate(template, func(ctx context.Context, request mcp.ReadResourceRequest) ([]interface{}, error) {
258+
userID := request.Params.URI // Extract ID from the full URI
259259

260260
profile, err := getUserProfile(userID) // Your DB/API call here
261261
if err != nil {
@@ -303,10 +303,10 @@ calculatorTool := mcp.NewTool("calculate",
303303
),
304304
)
305305

306-
s.AddTool(calculatorTool, func(args map[string]interface{}) (*mcp.CallToolResult, error) {
307-
op := args["operation"].(string)
308-
x := args["x"].(float64)
309-
y := args["y"].(float64)
306+
s.AddTool(calculatorTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
307+
op := request.Params.Arguments["operation"].(string)
308+
x := request.Params.Arguments["x"].(float64)
309+
y := request.Params.Arguments["y"].(float64)
310310

311311
var result float64
312312
switch op {
@@ -346,11 +346,11 @@ httpTool := mcp.NewTool("http_request",
346346
),
347347
)
348348

349-
s.AddTool(httpTool, func(args map[string]interface{}) (*mcp.CallToolResult, error) {
350-
method := args["method"].(string)
351-
url := args["url"].(string)
349+
s.AddTool(httpTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
350+
method := request.Params.Arguments["method"].(string)
351+
url := request.Params.Arguments["url"].(string)
352352
body := ""
353-
if b, ok := args["body"].(string); ok {
353+
if b, ok := request.Params.Arguments["body"].(string); ok {
354354
body = b
355355
}
356356

@@ -413,8 +413,8 @@ s.AddPrompt(mcp.NewPrompt("greeting",
413413
mcp.WithArgument("name",
414414
mcp.ArgumentDescription("Name of the person to greet"),
415415
),
416-
), func(args map[string]string) (*mcp.GetPromptResult, error) {
417-
name := args["name"]
416+
), func(ctx context.Context, request mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
417+
name := request.Params.Arguments["name"].(string)
418418
if name == "" {
419419
name = "friend"
420420
}
@@ -437,8 +437,8 @@ s.AddPrompt(mcp.NewPrompt("code_review",
437437
mcp.ArgumentDescription("Pull request number to review"),
438438
mcp.RequiredArgument(),
439439
),
440-
), func(args map[string]string) (*mcp.GetPromptResult, error) {
441-
prNumber := args["pr_number"]
440+
), func(ctx context.Context, request mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
441+
prNumber := request.Params.Arguments["pr_number"].(string)
442442
if prNumber == "" {
443443
return nil, fmt.Errorf("pr_number is required")
444444
}
@@ -468,8 +468,8 @@ s.AddPrompt(mcp.NewPrompt("query_builder",
468468
mcp.ArgumentDescription("Name of the table to query"),
469469
mcp.RequiredArgument(),
470470
),
471-
), func(args map[string]string) (*mcp.GetPromptResult, error) {
472-
tableName := args["table"]
471+
), func(ctx context.Context, request mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
472+
tableName := request.Params.Arguments["table"].(string)
473473
if tableName == "" {
474474
return nil, fmt.Errorf("table name is required")
475475
}

examples/everything/main.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"flag"
66
"fmt"
77
"log"
8+
"os"
89
"time"
910

1011
"github.com/mark3labs/mcp-go/mcp"
@@ -352,32 +353,24 @@ func (s *MCPServer) handleLongRunningOperationTool(
352353
request mcp.CallToolRequest,
353354
) (*mcp.CallToolResult, error) {
354355
arguments := request.Params.Arguments
356+
fmt.Fprintln(os.Stderr, request)
357+
progressToken := request.Params.Meta.ProgressToken
355358
duration, _ := arguments["duration"].(float64)
356359
steps, _ := arguments["steps"].(float64)
357360
stepDuration := duration / steps
358-
progressToken, _ := arguments["_meta"].(map[string]interface{})["progressToken"].(mcp.ProgressToken)
361+
server := server.ServerFromContext(ctx)
359362

360363
for i := 1; i < int(steps)+1; i++ {
361364
time.Sleep(time.Duration(stepDuration * float64(time.Second)))
362365
if progressToken != nil {
363-
// s.server.HandleMessage(
364-
// context.Background(),
365-
// mcp.JSONRPCNotification{
366-
// JSONRPC: mcp.JSONRPC_VERSION,
367-
// Notification: mcp.Notification{
368-
// Method: "progress",
369-
// Params: struct {
370-
// Meta map[string]interface{} `json:"_meta,omitempty"`
371-
// }{
372-
// Meta: map[string]interface{}{
373-
// "progress": i,
374-
// "total": int(steps),
375-
// "progressToken": progressToken,
376-
// },
377-
// },
378-
// },
379-
// },
380-
// )
366+
server.SendNotificationToClient(
367+
"notifications/progress",
368+
map[string]interface{}{
369+
"progress": i,
370+
"total": int(steps),
371+
"progressToken": progressToken,
372+
},
373+
)
381374
}
382375
}
383376

mcp/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,15 @@ type CallToolRequest struct {
616616
Params struct {
617617
Name string `json:"name"`
618618
Arguments map[string]interface{} `json:"arguments,omitempty"`
619+
Meta *struct {
620+
// If specified, the caller is requesting out-of-band progress
621+
// notifications for this request (as represented by
622+
// notifications/progress). The value of this parameter is an
623+
// opaque token that will be attached to any subsequent
624+
// notifications. The receiver is not obligated to provide these
625+
// notifications.
626+
ProgressToken ProgressToken `json:"progressToken,omitempty"`
627+
} `json:"_meta,omitempty"`
619628
} `json:"params"`
620629
}
621630

0 commit comments

Comments
 (0)