@@ -2,6 +2,7 @@ package mcp
22
33import (
44 "context"
5+ "os/exec"
56
67 "github.com/modelcontextprotocol/go-sdk/mcp"
78)
@@ -10,6 +11,22 @@ var templateRepos = []string{
1011 "https://github.com/functions-dev/templates" ,
1112}
1213
14+ // Executor abstracts command execution for testability
15+ type Executor interface {
16+ Execute (ctx context.Context , dir string , name string , args ... string ) ([]byte , error )
17+ }
18+
19+ // binaryExecutor implements Executor using os/exec
20+ type binaryExecutor struct {}
21+
22+ func (e binaryExecutor ) Execute (ctx context.Context , dir string , name string , args ... string ) ([]byte , error ) {
23+ cmd := exec .CommandContext (ctx , name , args ... )
24+ if dir != "" {
25+ cmd .Dir = dir
26+ }
27+ return cmd .CombinedOutput ()
28+ }
29+
1330type Server struct {
1431 impl * mcp.Server
1532 prefix string // Command prefix to use (e.g., "func" or "kn func")
@@ -26,12 +43,12 @@ type tool interface {
2643
2744type resource interface {
2845 desc () * mcp.Resource
29- handler ( prefix string ) mcp.ResourceHandler
46+ handle (context. Context , * mcp. ReadResourceRequest , string , Executor ) ( * mcp.ReadResourceResult , error )
3047}
3148
3249type prompt interface {
3350 desc () * mcp.Prompt
34- handler ( prefix string ) mcp.PromptHandler
51+ handle (context. Context , * mcp. GetPromptRequest , string ) ( * mcp.GetPromptResult , error )
3552}
3653
3754type Option func (* Server )
@@ -71,23 +88,22 @@ func New(options ...Option) *Server {
7188 },
7289 resources : []resource {
7390 rootHelpResource {},
74- cmdHelpResource {[]string {"create" }, "func://create/docs" },
75- cmdHelpResource {[]string {"build" }, "func://build/docs" },
76- cmdHelpResource {[]string {"deploy" }, "func://deploy/docs" },
77- cmdHelpResource {[]string {"list" }, "func://list/docs" },
78- cmdHelpResource {[]string {"delete" }, "func://delete/docs" },
79- cmdHelpResource {[]string {"config" , "volumes" , "add" }, "func://config/volumes/add/docs" },
80- cmdHelpResource {[]string {"config" , "volumes" , "remove" }, "func://config/volumes/remove/docs" },
81- cmdHelpResource {[]string {"config" , "labels" , "add" }, "func://config/labels/add/docs" },
82- cmdHelpResource {[]string {"config" , "labels" , "remove" }, "func://config/labels/remove/docs" },
83- cmdHelpResource {[]string {"config" , "envs" , "add" }, "func://config/envs/add/docs" },
84- cmdHelpResource {[]string {"config" , "envs" , "remove" }, "func://config/envs/remove/docs" },
91+ cmdHelpResource {[]string {"create" }, "function://help/create" },
92+ cmdHelpResource {[]string {"build" }, "function://help/build" },
93+ cmdHelpResource {[]string {"deploy" }, "function://help/deploy" },
94+ cmdHelpResource {[]string {"list" }, "function://help/list" },
95+ cmdHelpResource {[]string {"delete" }, "function://help/delete" },
96+ cmdHelpResource {[]string {"config" , "volumes" , "add" }, "function://help/config/volumes/add" },
97+ cmdHelpResource {[]string {"config" , "volumes" , "remove" }, "function://help/config/volumes/remove" },
98+ cmdHelpResource {[]string {"config" , "labels" , "add" }, "function://help/config/labels/add" },
99+ cmdHelpResource {[]string {"config" , "labels" , "remove" }, "function://help/config/labels/remove" },
100+ cmdHelpResource {[]string {"config" , "envs" , "add" }, "function://help/config/envs/add" },
101+ cmdHelpResource {[]string {"config" , "envs" , "remove" }, "function://help/config/envs/remove" },
102+ currentFunctionResource {},
85103 templatesResource {},
86104 },
87105 prompts : []prompt {
88- helpPrompt {},
89- cmdHelpPrompt {},
90- listTemplatesPrompt {},
106+ createFunctionPrompt {},
91107 },
92108 }
93109
@@ -100,11 +116,11 @@ func New(options ...Option) *Server {
100116 }
101117
102118 for _ , resource := range s .resources {
103- s .impl .AddResource (resource .desc (), resource . handler (s .prefix ))
119+ s .impl .AddResource (resource .desc (), withResourcePrefix (s .prefix , s . executor , resource . handle ))
104120 }
105121
106122 for _ , prompt := range s .prompts {
107- s .impl .AddPrompt (prompt .desc (), prompt . handler (s .prefix ))
123+ s .impl .AddPrompt (prompt .desc (), withPromptPrefix (s .prefix , prompt . handle ))
108124 }
109125
110126 return s
0 commit comments