-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for prompts, notifications and context.Context #1
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"golang.org/x/time/rate" | ||
|
||
|
@@ -37,21 +39,51 @@ func main() { | |
}, | ||
} | ||
|
||
s := mcp.NewServer(serverInfo, tools) | ||
prompts := []mcp.PromptDefinition{ | ||
{ | ||
Metadata: mcp.Prompt{ | ||
Name: "example", | ||
Description: ptr("An example prompt template"), | ||
Arguments: []mcp.PromptArgument{ | ||
{ | ||
Name: "text", | ||
Description: ptr("Text to process"), | ||
Required: ptr(true), | ||
}, | ||
}, | ||
}, | ||
Process: processPrompt, | ||
RateLimit: rate.NewLimiter(rate.Every(time.Second), 5), | ||
}, | ||
} | ||
|
||
s := mcp.NewServer(serverInfo, tools, prompts) | ||
s.Serve() | ||
} | ||
|
||
func computeSHA256(params mcp.CallToolRequestParams) (mcp.CallToolResult, error) { | ||
func ptr[T any](t T) *T { | ||
return &t | ||
} | ||
|
||
// Update the computeSHA256 function to send a single notification | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment looks like a TODO item. It could be removed or changed to document the function. |
||
func computeSHA256(ctx context.Context, n mcp.Notifier, params mcp.CallToolRequestParams) (mcp.CallToolResult, error) { | ||
txt := params.Arguments["text"].(string) | ||
|
||
if len(txt) == 0 { | ||
return mcp.CallToolResult{}, errors.New("failed to compute checksum: text cannot be empty") | ||
} | ||
|
||
err := n.Notify(ctx, "test/notification", map[string]any{ | ||
"message": "Processing text", | ||
}) | ||
if err != nil { | ||
fmt.Printf("Failed to send notification: %v\n", err) | ||
} | ||
|
||
h := sha256.New() | ||
h.Write([]byte(txt)) | ||
|
||
checksum := fmt.Sprintf("%x", h.Sum(nil)) | ||
|
||
var noError bool | ||
return mcp.CallToolResult{ | ||
Content: []any{ | ||
|
@@ -63,3 +95,28 @@ func computeSHA256(params mcp.CallToolRequestParams) (mcp.CallToolResult, error) | |
IsError: &noError, | ||
}, nil | ||
} | ||
|
||
func processPrompt(ctx context.Context, n mcp.Notifier, params mcp.GetPromptRequestParams) (mcp.GetPromptResult, error) { | ||
if params.Arguments["text"] == "" { | ||
return mcp.GetPromptResult{}, errors.New("input text cannot be empty") | ||
} | ||
|
||
err := n.Notify(ctx, "test/notification", map[string]any{ | ||
"message": "Processing text", | ||
}) | ||
if err != nil { | ||
fmt.Printf("Failed to send notification: %v\n", err) | ||
} | ||
|
||
return mcp.GetPromptResult{ | ||
Messages: []mcp.PromptMessage{ | ||
{ | ||
Role: mcp.RoleAssistant, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would a role of |
||
Content: mcp.TextContent{ | ||
Type: "text", | ||
Text: "Processed: " + params.Arguments["text"], | ||
}, | ||
}, | ||
}, | ||
}, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spec doesn't require rate limiting of prompt gets in the same way that it does for tool calls. However having a rate limit seems reasonable.