@@ -14,109 +14,136 @@ import (
14
14
)
15
15
16
16
// getRepositoryContent defines the resource template and handler for the Repository Content API.
17
- func getRepositoryContent (client * github.Client ,t translations.TranslationHelperFunc ) (mainTemplate mcp.ResourceTemplate ,reftemplate mcp.ResourceTemplate ,shaTemplate mcp.ResourceTemplate ,tagTemplate mcp.ResourceTemplate ,prTemplate mcp.ResourceTemplate ,handler server.ResourceTemplateHandlerFunc ) {
18
-
17
+ func getRepositoryContent (client * github.Client ,t translations.TranslationHelperFunc ) (mcp.ResourceTemplate , server.ResourceTemplateHandlerFunc ) {
19
18
return mcp .NewResourceTemplate (
20
19
"repo://{owner}/{repo}/contents{/path*}" ,// Resource template
21
20
t ("RESOURCE_REPOSITORY_CONTENT_DESCRIPTION" ,"Repository Content" ),
22
- ),mcp .NewResourceTemplate (
21
+ ),
22
+ handlerFunc (client ,t )
23
+ }
24
+
25
+ // getRepositoryContent defines the resource template and handler for the Repository Content API.
26
+ func getRepositoryBranchContent (client * github.Client ,t translations.TranslationHelperFunc ) (mcp.ResourceTemplate , server.ResourceTemplateHandlerFunc ) {
27
+ return mcp .NewResourceTemplate (
23
28
"repo://{owner}/{repo}/refs/heads/{branch}/contents{/path*}" ,// Resource template
24
29
t ("RESOURCE_REPOSITORY_CONTENT_BRANCH_DESCRIPTION" ,"Repository Content for specific branch" ),
25
- ),mcp .NewResourceTemplate (
30
+ ),
31
+ handlerFunc (client ,t )
32
+ }
33
+
34
+ // getRepositoryContent defines the resource template and handler for the Repository Content API.
35
+ func getRepositoryCommitContent (client * github.Client ,t translations.TranslationHelperFunc ) (mcp.ResourceTemplate , server.ResourceTemplateHandlerFunc ) {
36
+ return mcp .NewResourceTemplate (
26
37
"repo://{owner}/{repo}/sha/{sha}/contents{/path*}" ,// Resource template
27
38
t ("RESOURCE_REPOSITORY_CONTENT_COMMIT_DESCRIPTION" ,"Repository Content for specific commit" ),
28
- ),mcp .NewResourceTemplate (
39
+ ),
40
+ handlerFunc (client ,t )
41
+ }
42
+
43
+ // getRepositoryContent defines the resource template and handler for the Repository Content API.
44
+ func getRepositoryTagContent (client * github.Client ,t translations.TranslationHelperFunc ) (mcp.ResourceTemplate , server.ResourceTemplateHandlerFunc ) {
45
+ return mcp .NewResourceTemplate (
29
46
"repo://{owner}/{repo}/refs/tags/{tag}/contents{/path*}" ,// Resource template
30
47
t ("RESOURCE_REPOSITORY_CONTENT_TAG_DESCRIPTION" ,"Repository Content for specific tag" ),
31
- ),mcp .NewResourceTemplate (
48
+ ),
49
+ handlerFunc (client ,t )
50
+ }
51
+
52
+ // getRepositoryContent defines the resource template and handler for the Repository Content API.
53
+ func getRepositoryPrContent (client * github.Client ,t translations.TranslationHelperFunc ) (mcp.ResourceTemplate , server.ResourceTemplateHandlerFunc ) {
54
+ return mcp .NewResourceTemplate (
32
55
"repo://{owner}/{repo}/refs/pull/{pr_number}/head/contents{/path*}" ,// Resource template
33
56
t ("RESOURCE_REPOSITORY_CONTENT_PR_DESCRIPTION" ,"Repository Content for specific pull request" ),
34
- ),func (ctx context.Context ,request mcp.ReadResourceRequest ) ([]mcp.ResourceContents ,error ) {
35
- // Extract parameters from request.Params.URI
57
+ ),
58
+ handlerFunc (client ,t )
59
+ }
36
60
37
- owner := request .Params .Arguments ["owner" ].([]string )[0 ]
38
- repo := request .Params .Arguments ["repo" ].([]string )[0 ]
39
- // path should be a joined list of the path parts
40
- path := strings .Join (request .Params .Arguments ["path" ].([]string ),"/" )
61
+ func handlerFunc (client * github.Client ,_ translations.TranslationHelperFunc )func (ctx context.Context ,request mcp.ReadResourceRequest ) ([]mcp.ResourceContents ,error ) {
62
+ return func (ctx context.Context ,request mcp.ReadResourceRequest ) ([]mcp.ResourceContents ,error ) {// Extract parameters from request.Params.URI
41
63
42
- opts := & github.RepositoryContentGetOptions {}
64
+ owner := request .Params .Arguments ["owner" ].([]string )[0 ]
65
+ repo := request .Params .Arguments ["repo" ].([]string )[0 ]
66
+ // path should be a joined list of the path parts
67
+ path := strings .Join (request .Params .Arguments ["path" ].([]string ),"/" )
43
68
44
- sha ,ok := request .Params .Arguments ["sha" ].([]string )
45
- if ok {
46
- opts .Ref = sha [0 ]
47
- }
69
+ opts := & github.RepositoryContentGetOptions {}
48
70
49
- branch ,ok := request .Params .Arguments ["branch " ].([]string )
50
- if ok {
51
- opts .Ref = "refs/heads/" + branch [0 ]
52
- }
71
+ sha ,ok := request .Params .Arguments ["sha " ].([]string )
72
+ if ok {
73
+ opts .Ref = sha [0 ]
74
+ }
53
75
54
- tag ,ok := request .Params .Arguments ["tag" ].([]string )
55
- if ok {
56
- opts .Ref = "refs/tags/" + tag [0 ]
57
- }
58
- prNumber ,ok := request .Params .Arguments ["pr_number" ].([]string )
59
- if ok {
60
- opts .Ref = "refs/pull/" + prNumber [0 ]+ "/head"
61
- }
76
+ branch ,ok := request .Params .Arguments ["branch" ].([]string )
77
+ if ok {
78
+ opts .Ref = "refs/heads/" + branch [0 ]
79
+ }
80
+
81
+ tag ,ok := request .Params .Arguments ["tag" ].([]string )
82
+ if ok {
83
+ opts .Ref = "refs/tags/" + tag [0 ]
84
+ }
85
+ prNumber ,ok := request .Params .Arguments ["pr_number" ].([]string )
86
+ if ok {
87
+ opts .Ref = "refs/pull/" + prNumber [0 ]+ "/head"
88
+ }
89
+
90
+ // Use the GitHub client to fetch repository content
91
+ fileContent ,directoryContent ,_ ,err := client .Repositories .GetContents (ctx ,owner ,repo ,path ,opts )
92
+ if err != nil {
93
+ return nil ,err
94
+ }
95
+
96
+ if directoryContent != nil {
97
+ // Process the directory content and return it as resource contents
98
+ var resources []mcp.ResourceContents
99
+ for _ ,entry := range directoryContent {
100
+ mimeType := "text/directory"
101
+ if entry .GetType ()== "file" {
102
+ mimeType = mime .TypeByExtension (filepath .Ext (entry .GetName ()))
103
+ }
104
+ resources = append (resources , mcp.TextResourceContents {
105
+ URI :entry .GetHTMLURL (),
106
+ MIMEType :mimeType ,
107
+ Text :entry .GetName (),
108
+ })
62
109
63
- // Use the GitHub client to fetch repository content
64
- fileContent ,directoryContent ,_ ,err := client .Repositories .GetContents (ctx ,owner ,repo ,path ,opts )
65
- if err != nil {
66
- return nil ,err
67
110
}
111
+ return resources ,nil
68
112
69
- if directoryContent != nil {
70
- // Process the directory content and return it as resource contents
71
- var resources []mcp.ResourceContents
72
- for _ ,entry := range directoryContent {
73
- mimeType := "text/directory"
74
- if entry .GetType ()== "file" {
75
- mimeType = mime .TypeByExtension (filepath .Ext (entry .GetName ()))
76
- }
77
- resources = append (resources , mcp.TextResourceContents {
78
- URI :entry .GetHTMLURL (),
79
- MIMEType :mimeType ,
80
- Text :entry .GetName (),
81
- })
113
+ }else if fileContent != nil {
114
+ // Process the file content and return it as a binary resource
82
115
116
+ if fileContent .Content != nil {
117
+ decodedContent ,err := fileContent .GetContent ()
118
+ if err != nil {
119
+ return nil ,err
83
120
}
84
- return resources ,nil
85
-
86
- }else if fileContent != nil {
87
- // Process the file content and return it as a binary resource
88
-
89
- if fileContent .Content != nil {
90
- decodedContent ,err := fileContent .GetContent ()
91
- if err != nil {
92
- return nil ,err
93
- }
94
-
95
- mimeType := mime .TypeByExtension (filepath .Ext (fileContent .GetName ()))
96
-
97
- // Check if the file is text-based
98
- if strings .HasPrefix (mimeType ,"text" ) {
99
- // Return as TextResourceContents
100
- return []mcp.ResourceContents {
101
- mcp.TextResourceContents {
102
- URI :request .Params .URI ,
103
- MIMEType :mimeType ,
104
- Text :decodedContent ,
105
- },
106
- },nil
107
- }
108
-
109
- // Otherwise, return as BlobResourceContents
121
+
122
+ mimeType := mime .TypeByExtension (filepath .Ext (fileContent .GetName ()))
123
+
124
+ // Check if the file is text-based
125
+ if strings .HasPrefix (mimeType ,"text" ) {
126
+ // Return as TextResourceContents
110
127
return []mcp.ResourceContents {
111
- mcp.BlobResourceContents {
128
+ mcp.TextResourceContents {
112
129
URI :request .Params .URI ,
113
130
MIMEType :mimeType ,
114
- Blob :base64 . StdEncoding . EncodeToString ([] byte ( decodedContent )), // Encode content as Base64
131
+ Text :decodedContent ,
115
132
},
116
133
},nil
117
134
}
118
- }
119
135
120
- return nil ,nil
136
+ // Otherwise, return as BlobResourceContents
137
+ return []mcp.ResourceContents {
138
+ mcp.BlobResourceContents {
139
+ URI :request .Params .URI ,
140
+ MIMEType :mimeType ,
141
+ Blob :base64 .StdEncoding .EncodeToString ([]byte (decodedContent )),// Encode content as Base64
142
+ },
143
+ },nil
144
+ }
121
145
}
146
+
147
+ return nil ,nil
148
+ }
122
149
}