6119
My
mysis / files
mysis / internal / mcp / tools.go
4.2 KB
1package mcp 2 3import ( 4 "context" 5 "encoding/json" 6 "fmt" 7) 8 9// CredentialStore defines the interface for storing and retrieving session credentials. 10type CredentialStore interface { 11 SaveCredentials(sessionID, username, password string) error 12 GetCredentials(sessionID string) (username, password string, err error) 13} 14 15// SaveCredentialsArgs represents arguments for save_credentials tool. 16type SaveCredentialsArgs struct { 17 Username string `json:"username"` 18 Password string `json:"password"` 19} 20 21// GetCredentialsResult represents the result of get_credentials tool. 22type GetCredentialsResult struct { 23 Username string `json:"username"` 24 Password string `json:"password"` 25} 26 27// NewSaveCredentialsTool creates the save_credentials tool definition. 28func NewSaveCredentialsTool() Tool { 29 schema := map[string]interface{}{ 30 "type": "object", 31 "properties": map[string]interface{}{ 32 "username": map[string]interface{}{ 33 "type": "string", 34 "description": "Username for the game account", 35 }, 36 "password": map[string]interface{}{ 37 "type": "string", 38 "description": "Password for the game account", 39 }, 40 }, 41 "required": []string{"username", "password"}, 42 } 43 44 schemaJSON, _ := json.Marshal(schema) 45 46 return Tool{ 47 Name: "save_credentials", 48 Description: "Save game credentials (username and password) for the current session. Credentials are stored securely and scoped to this session only.", 49 InputSchema: schemaJSON, 50 } 51} 52 53// NewGetCredentialsTool creates the get_credentials tool definition. 54func NewGetCredentialsTool() Tool { 55 schema := map[string]interface{}{ 56 "type": "object", 57 "properties": map[string]interface{}{}, 58 } 59 60 schemaJSON, _ := json.Marshal(schema) 61 62 return Tool{ 63 Name: "get_credentials", 64 Description: "Retrieve saved game credentials (username and password) for the current session. Returns empty strings if no credentials are saved.", 65 InputSchema: schemaJSON, 66 } 67} 68 69// MakeSaveCredentialsHandler creates a handler for save_credentials tool. 70func MakeSaveCredentialsHandler(store CredentialStore, sessionID string) ToolHandler { 71 return func(ctx context.Context, arguments json.RawMessage) (*ToolResult, error) { 72 var args SaveCredentialsArgs 73 if err := json.Unmarshal(arguments, &args); err != nil { 74 return &ToolResult{ 75 Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("Invalid arguments: %v", err)}}, 76 IsError: true, 77 }, nil 78 } 79 80 if args.Username == "" { 81 return &ToolResult{ 82 Content: []ContentBlock{{Type: "text", Text: "Username cannot be empty"}}, 83 IsError: true, 84 }, nil 85 } 86 87 if args.Password == "" { 88 return &ToolResult{ 89 Content: []ContentBlock{{Type: "text", Text: "Password cannot be empty"}}, 90 IsError: true, 91 }, nil 92 } 93 94 if err := store.SaveCredentials(sessionID, args.Username, args.Password); err != nil { 95 return &ToolResult{ 96 Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("Failed to save credentials: %v", err)}}, 97 IsError: true, 98 }, nil 99 } 100 101 return &ToolResult{ 102 Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("Credentials saved successfully for user '%s'", args.Username)}}, 103 IsError: false, 104 }, nil 105 } 106} 107 108// MakeGetCredentialsHandler creates a handler for get_credentials tool. 109func MakeGetCredentialsHandler(store CredentialStore, sessionID string) ToolHandler { 110 return func(ctx context.Context, arguments json.RawMessage) (*ToolResult, error) { 111 username, password, err := store.GetCredentials(sessionID) 112 if err != nil { 113 return &ToolResult{ 114 Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("Failed to retrieve credentials: %v", err)}}, 115 IsError: true, 116 }, nil 117 } 118 119 if username == "" && password == "" { 120 return &ToolResult{ 121 Content: []ContentBlock{{Type: "text", Text: "No credentials saved for this session"}}, 122 IsError: false, 123 }, nil 124 } 125 126 result := GetCredentialsResult{ 127 Username: username, 128 Password: password, 129 } 130 131 resultJSON, err := json.Marshal(result) 132 if err != nil { 133 return &ToolResult{ 134 Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("Failed to format credentials: %v", err)}}, 135 IsError: true, 136 }, nil 137 } 138 139 return &ToolResult{ 140 Content: []ContentBlock{{Type: "text", Text: string(resultJSON)}}, 141 IsError: false, 142 }, nil 143 } 144}