@@ -6,13 +6,6 @@ package mcp
6
6
7
7
import (
8
8
"context"
9
- "encoding/json"
10
- "fmt"
11
- "reflect"
12
- "slices"
13
-
14
- "golang.org/x/tools/internal/mcp/internal/util"
15
- "golang.org/x/tools/internal/mcp/jsonschema"
16
9
)
17
10
18
11
// A PromptHandler handles a call to prompts/get.
@@ -23,107 +16,3 @@ type ServerPrompt struct {
23
16
Prompt * Prompt
24
17
Handler PromptHandler
25
18
}
26
-
27
- // NewServerPrompt is a helper that uses reflection to create a prompt for the
28
- // given handler.
29
- //
30
- // The arguments for the prompt are extracted from the request type for the
31
- // handler. The handler request type must be a struct consisting only of fields
32
- // of type string or *string. The argument names for the resulting prompt
33
- // definition correspond to the JSON names of the request fields, and any
34
- // fields that are not marked "omitempty" are considered required.
35
- //
36
- // The handler is passed [GetPromptParams] so it can have access to prompt
37
- // parameters other than name and arguments. At present, there are no such
38
- // parameters.
39
- func NewServerPrompt [In any ](name , description string , handler func (context.Context , * ServerSession , In , * GetPromptParams ) (* GetPromptResult , error ), opts ... PromptOption ) * ServerPrompt {
40
- schema , err := jsonschema .For [In ]()
41
- if err != nil {
42
- panic (err )
43
- }
44
- if schema .Type != "object" || ! reflect .DeepEqual (schema .AdditionalProperties , & jsonschema.Schema {Not : & jsonschema.Schema {}}) {
45
- panic (fmt .Sprintf ("invalid input type %q: handler input type must be a struct" , schema .Type ))
46
- }
47
- resolved , err := schema .Resolve (nil )
48
- if err != nil {
49
- panic (err )
50
- }
51
- prompt := & ServerPrompt {
52
- Prompt : & Prompt {
53
- Name : name ,
54
- Description : description ,
55
- },
56
- }
57
- required := make (map [string ]bool )
58
- for _ , p := range schema .Required {
59
- required [p ] = true
60
- }
61
- for name , prop := range util .Sorted (schema .Properties ) {
62
- if prop .Type != "string" {
63
- panic (fmt .Sprintf ("invalid property type %q: handler type must consist only of string fields" , prop .Type ))
64
- }
65
- prompt .Prompt .Arguments = append (prompt .Prompt .Arguments , & PromptArgument {
66
- Name : name ,
67
- Description : prop .Description ,
68
- Required : required [name ],
69
- })
70
- }
71
- prompt .Handler = func (ctx context.Context , ss * ServerSession , params * GetPromptParams ) (* GetPromptResult , error ) {
72
- // For simplicity, just marshal and unmarshal the arguments.
73
- // This could be avoided in the future.
74
- rawArgs , err := json .Marshal (params .Arguments )
75
- if err != nil {
76
- return nil , err
77
- }
78
- var args In
79
- if err := unmarshalSchema (rawArgs , resolved , & args ); err != nil {
80
- return nil , err
81
- }
82
- return handler (ctx , ss , args , params )
83
- }
84
- for _ , opt := range opts {
85
- opt .set (prompt )
86
- }
87
- return prompt
88
- }
89
-
90
- // A PromptOption configures the behavior of a Prompt.
91
- type PromptOption interface {
92
- set (* ServerPrompt )
93
- }
94
-
95
- type promptSetter func (* ServerPrompt )
96
-
97
- func (s promptSetter ) set (p * ServerPrompt ) { s (p ) }
98
-
99
- // Argument configures the 'schema' of a prompt argument.
100
- // If the argument does not exist, it is added.
101
- //
102
- // Since prompt arguments are not a full JSON schema, Argument only accepts
103
- // Required and Description, and panics when encountering any other option.
104
- func Argument (name string , opts ... SchemaOption ) PromptOption {
105
- return promptSetter (func (p * ServerPrompt ) {
106
- i := slices .IndexFunc (p .Prompt .Arguments , func (arg * PromptArgument ) bool {
107
- return arg .Name == name
108
- })
109
- var arg * PromptArgument
110
- if i < 0 {
111
- i = len (p .Prompt .Arguments )
112
- arg = & PromptArgument {Name : name }
113
- p .Prompt .Arguments = append (p .Prompt .Arguments , arg )
114
- } else {
115
- arg = p .Prompt .Arguments [i ]
116
- }
117
- for _ , opt := range opts {
118
- switch v := opt .(type ) {
119
- case required :
120
- arg .Required = bool (v )
121
- case description :
122
- arg .Description = string (v )
123
- default :
124
- panic (fmt .Sprintf ("unsupported prompt argument schema option %T" , opt ))
125
- }
126
- }
127
- p .Prompt .Arguments [i ] = arg
128
- })
129
- }
0 commit comments