@@ -13,6 +13,9 @@ import (
1313 "github.com/mark3labs/mcp-go/mcp"
1414)
1515
16+ var ErrToolExecutionDenied = fmt .Errorf ("tool execution denied by user" )
17+ var ErrSamplingDenied = fmt .Errorf ("sampling denied by user" )
18+
1619// ConsentChecker provides shared consent checking logic for different tool types
1720type ConsentChecker struct {
1821 consentMgr ConsentManager
@@ -66,6 +69,49 @@ func (cc *ConsentChecker) CheckSamplingConsent(
6669 return cc .consentMgr .CheckConsent (ctx , consentRequest )
6770}
6871
72+ // PromptAndGrantConsent shows consent prompt and grants permission based on user choice
73+ func (cc * ConsentChecker ) PromptAndGrantConsent (
74+ ctx context.Context ,
75+ toolName , toolDesc string ,
76+ annotations mcp.ToolAnnotation ,
77+ ) error {
78+ toolId := fmt .Sprintf ("%s/%s" , cc .serverName , toolName )
79+
80+ choice , err := cc .promptForToolConsent (ctx , toolName , toolDesc , annotations )
81+ if err != nil {
82+ return err
83+ }
84+
85+ if choice == "deny" {
86+ return ErrToolExecutionDenied
87+ }
88+
89+ // Grant consent based on user choice
90+ return cc .grantConsentFromChoice (ctx , toolId , choice , OperationTypeTool )
91+ }
92+
93+ // PromptAndGrantSamplingConsent shows sampling consent prompt and grants permission based on user choice
94+ func (cc * ConsentChecker ) PromptAndGrantSamplingConsent (
95+ ctx context.Context ,
96+ toolName , toolDesc string ,
97+ ) error {
98+ toolId := fmt .Sprintf ("%s/%s" , cc .serverName , toolName )
99+
100+ choice , err := cc .promptForSamplingConsent (ctx , toolName , toolDesc )
101+ if err != nil {
102+ return fmt .Errorf ("sampling consent prompt failed: %w" , err )
103+ }
104+
105+ if choice == "deny" {
106+ return ErrSamplingDenied
107+ }
108+
109+ // Grant sampling consent based on user choice
110+ return cc .grantConsentFromChoice (ctx , toolId , choice , OperationTypeSampling )
111+ }
112+
113+ // Private Struct Methods
114+
69115// formatToolDescriptionWithAnnotations creates a formatted description with tool annotations as bullet points
70116func (cc * ConsentChecker ) formatToolDescriptionWithAnnotations (
71117 toolDesc string ,
@@ -128,27 +174,6 @@ func (cc *ConsentChecker) formatToolDescriptionWithAnnotations(
128174 return description
129175}
130176
131- // PromptAndGrantConsent shows consent prompt and grants permission based on user choice
132- func (cc * ConsentChecker ) PromptAndGrantConsent (
133- ctx context.Context ,
134- toolName , toolDesc string ,
135- annotations mcp.ToolAnnotation ,
136- ) error {
137- toolId := fmt .Sprintf ("%s/%s" , cc .serverName , toolName )
138-
139- choice , err := cc .promptForToolConsent (ctx , toolName , toolDesc , annotations )
140- if err != nil {
141- return fmt .Errorf ("consent prompt failed: %w" , err )
142- }
143-
144- if choice == "deny" {
145- return fmt .Errorf ("tool execution denied by user" )
146- }
147-
148- // Grant consent based on user choice
149- return cc .grantConsentFromChoice (ctx , toolId , choice , OperationTypeTool )
150- }
151-
152177// promptForToolConsent shows an interactive consent prompt and returns the user's choice
153178func (cc * ConsentChecker ) promptForToolConsent (
154179 ctx context.Context ,
@@ -361,26 +386,6 @@ func (cc *ConsentChecker) grantConsentFromChoice(
361386 return cc .consentMgr .GrantConsent (ctx , rule )
362387}
363388
364- // PromptAndGrantSamplingConsent shows sampling consent prompt and grants permission based on user choice
365- func (cc * ConsentChecker ) PromptAndGrantSamplingConsent (
366- ctx context.Context ,
367- toolName , toolDesc string ,
368- ) error {
369- toolId := fmt .Sprintf ("%s/%s" , cc .serverName , toolName )
370-
371- choice , err := cc .promptForSamplingConsent (ctx , toolName , toolDesc )
372- if err != nil {
373- return fmt .Errorf ("sampling consent prompt failed: %w" , err )
374- }
375-
376- if choice == "deny" {
377- return fmt .Errorf ("sampling denied by user" )
378- }
379-
380- // Grant sampling consent based on user choice
381- return cc .grantConsentFromChoice (ctx , toolId , choice , OperationTypeSampling )
382- }
383-
384389// promptForSamplingConsent shows an interactive sampling consent prompt and returns the user's choice
385390func (cc * ConsentChecker ) promptForSamplingConsent (
386391 ctx context.Context ,
0 commit comments