Commit f635690

mo khan <mo@mokhan.ca>
2025-06-22 20:03:27
feat: add fetch prompt to fetch server for manual URL entry
- Add registerPrompts method to register fetch prompt - Implement HandleFetchPrompt for interactive URL fetching - Support required url argument and optional reason argument - Generate conversation prompts for manual URL entry workflow - Include proper error handling for missing required arguments Features: - Interactive prompt for manual URL specification - Context-aware descriptions based on provided reason - User/assistant message flow to guide fetch tool usage - Proper argument validation and error messages Prompt supports: - url (required): The URL to fetch content from - reason (optional): Context for why the URL is being fetched Testing shows: - Proper prompt registration and listing - Correct message generation for both minimal and full arguments - Error handling for missing required URL parameter - Dynamic description updates based on reason context 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6148f00
Changed files (1)
pkg
pkg/fetch/server.go
@@ -44,8 +44,9 @@ func New() *Server {
 		htmlProcessor: htmlprocessor.NewContentExtractor(),
 	}
 
-	// Register all fetch tools
+	// Register all fetch tools and prompts
 	fetchServer.registerTools()
+	fetchServer.registerPrompts()
 
 	return fetchServer
 }
@@ -55,6 +56,28 @@ func (fs *Server) registerTools() {
 	fs.RegisterTool("fetch", fs.HandleFetch)
 }
 
+// registerPrompts registers all Fetch prompts with the server
+func (fs *Server) registerPrompts() {
+	fetchPrompt := mcp.Prompt{
+		Name:        "fetch",
+		Description: "Prompt for manually entering a URL to fetch content from",
+		Arguments: []mcp.PromptArgument{
+			{
+				Name:        "url",
+				Description: "The URL to fetch content from",
+				Required:    true,
+			},
+			{
+				Name:        "reason",
+				Description: "Why you want to fetch this URL (optional context)",
+				Required:    false,
+			},
+		},
+	}
+	
+	fs.RegisterPrompt(fetchPrompt, fs.HandleFetchPrompt)
+}
+
 // ListTools returns all available Fetch tools
 func (fs *Server) ListTools() []mcp.Tool {
 	return []mcp.Tool{
@@ -161,6 +184,51 @@ func (fs *Server) HandleFetch(req mcp.CallToolRequest) (mcp.CallToolResult, erro
 	return mcp.NewToolResult(mcp.NewTextContent(string(jsonResult))), nil
 }
 
+// Prompt handlers
+
+func (fs *Server) HandleFetchPrompt(req mcp.GetPromptRequest) (mcp.GetPromptResult, error) {
+	url, hasURL := req.Arguments["url"].(string)
+	reason, hasReason := req.Arguments["reason"].(string)
+
+	if !hasURL || url == "" {
+		return mcp.GetPromptResult{}, fmt.Errorf("url argument is required")
+	}
+
+	// Create the prompt messages
+	var messages []mcp.PromptMessage
+
+	// User message with the URL and optional reason
+	userContent := fmt.Sprintf("Please fetch the content from this URL: %s", url)
+	if hasReason && reason != "" {
+		userContent += fmt.Sprintf("\n\nReason: %s", reason)
+	}
+
+	messages = append(messages, mcp.PromptMessage{
+		Role:    "user",
+		Content: mcp.NewTextContent(userContent),
+	})
+
+	// Assistant message suggesting the fetch tool usage
+	assistantContent := fmt.Sprintf(`I'll fetch the content from %s for you.
+
+Let me use the fetch tool to retrieve and process the content:`, url)
+
+	messages = append(messages, mcp.PromptMessage{
+		Role:    "assistant", 
+		Content: mcp.NewTextContent(assistantContent),
+	})
+
+	description := "Manual URL fetch prompt"
+	if hasReason && reason != "" {
+		description = fmt.Sprintf("Manual URL fetch: %s", reason)
+	}
+
+	return mcp.GetPromptResult{
+		Description: description,
+		Messages:    messages,
+	}, nil
+}
+
 // Helper methods
 
 func (fs *Server) fetchContent(urlStr string, maxLength, startIndex int, raw bool) (*FetchResult, error) {