Skip to content

Commit 8cf1ae5

Browse files
committed
Address PR feedback
Signed-off-by: Juan Antonio Osorio <[email protected]>
1 parent db5b86b commit 8cf1ae5

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

pkg/mcp/parser.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"bytes"
66
"context"
77
"encoding/json"
8-
"fmt"
98
"io"
109
"net/http"
10+
"strconv"
1111
"strings"
1212

1313
"golang.org/x/exp/jsonrpc2"
@@ -297,7 +297,12 @@ func handleLoggingMethod(paramsMap map[string]interface{}) (string, map[string]i
297297
return "", nil
298298
}
299299

300-
// handleCompletionMethod extracts resource ID for completion requests
300+
// handleCompletionMethod extracts resource ID for completion requests.
301+
// For PromptReference: extracts the prompt name
302+
// For ResourceTemplateReference: extracts the template URI
303+
// For legacy string ref: returns the string value
304+
// Always returns paramsMap as arguments since completion requests need the full context
305+
// including the argument being completed and any context from previous completions.
301306
func handleCompletionMethod(paramsMap map[string]interface{}) (string, map[string]interface{}) {
302307
// Check if ref is a map (PromptReference or ResourceTemplateReference)
303308
if ref, ok := paramsMap["ref"].(map[string]interface{}); ok {
@@ -326,16 +331,29 @@ func handleElicitationMethod(paramsMap map[string]interface{}) (string, map[stri
326331
return "", paramsMap
327332
}
328333

329-
// handleSamplingMethod extracts resource ID for sampling/createMessage requests
334+
// handleSamplingMethod extracts resource ID for sampling/createMessage requests.
335+
// Returns the model name from modelPreferences if available, otherwise returns a
336+
// truncated version of the systemPrompt. The 50-character truncation provides a
337+
// reasonable balance between uniqueness and readability for authorization and audit logs.
330338
func handleSamplingMethod(paramsMap map[string]interface{}) (string, map[string]interface{}) {
331339
// Use model preferences or system prompt as identifier if available
332-
if modelPrefs, ok := paramsMap["modelPreferences"].(map[string]interface{}); ok {
333-
if name, ok := modelPrefs["name"].(string); ok {
340+
if modelPrefs, ok := paramsMap["modelPreferences"].(map[string]interface{}); ok && modelPrefs != nil {
341+
// Try direct name field first (simplified structure)
342+
if name, ok := modelPrefs["name"].(string); ok && name != "" {
334343
return name, paramsMap
335344
}
345+
// Try to get model name from hints array (full spec structure)
346+
if hints, ok := modelPrefs["hints"].([]interface{}); ok && len(hints) > 0 {
347+
if hint, ok := hints[0].(map[string]interface{}); ok {
348+
if name, ok := hint["name"].(string); ok && name != "" {
349+
return name, paramsMap
350+
}
351+
}
352+
}
336353
}
337354
if systemPrompt, ok := paramsMap["systemPrompt"].(string); ok && systemPrompt != "" {
338355
// Use first 50 chars of system prompt as identifier
356+
// This provides a reasonable balance between uniqueness and readability
339357
if len(systemPrompt) > 50 {
340358
return systemPrompt[:50], paramsMap
341359
}
@@ -360,27 +378,29 @@ func handleResourceUnsubscribeMethod(paramsMap map[string]interface{}) (string,
360378
return "", nil
361379
}
362380

363-
// handleProgressNotificationMethod extracts resource ID for progress notifications
381+
// handleProgressNotificationMethod extracts resource ID for progress notifications.
382+
// Extracts the progressToken which can be either a string or numeric value.
364383
func handleProgressNotificationMethod(paramsMap map[string]interface{}) (string, map[string]interface{}) {
365384
if token, ok := paramsMap["progressToken"].(string); ok {
366385
return token, paramsMap
367386
}
368387
// Also handle numeric progress tokens
369388
if token, ok := paramsMap["progressToken"].(float64); ok {
370-
return fmt.Sprintf("%.0f", token), paramsMap
389+
return strconv.FormatFloat(token, 'f', 0, 64), paramsMap
371390
}
372391
return "", paramsMap
373392
}
374393

375-
// handleCancelledNotificationMethod extracts resource ID for cancelled notifications
394+
// handleCancelledNotificationMethod extracts resource ID for cancelled notifications.
395+
// Extracts the requestId which can be either a string or numeric value.
376396
func handleCancelledNotificationMethod(paramsMap map[string]interface{}) (string, map[string]interface{}) {
377397
// Extract request ID as the resource identifier
378398
if requestId, ok := paramsMap["requestId"].(string); ok {
379399
return requestId, paramsMap
380400
}
381401
// Handle numeric request IDs
382402
if requestId, ok := paramsMap["requestId"].(float64); ok {
383-
return fmt.Sprintf("%.0f", requestId), paramsMap
403+
return strconv.FormatFloat(requestId, 'f', 0, 64), paramsMap
384404
}
385405
return "", paramsMap
386406
}

0 commit comments

Comments
 (0)