Skip to content

Commit 66da39a

Browse files
committed
✨ Language and region as resource template
issue #64
1 parent 8f55663 commit 66da39a

File tree

8 files changed

+90
-20
lines changed

8 files changed

+90
-20
lines changed

cmd/i18nLanguage/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ go_library(
1111
deps = [
1212
"//cmd",
1313
"//pkg/i18nLanguage",
14+
"//pkg/utils",
1415
"@com_github_mark3labs_mcp_go//mcp",
1516
"@com_github_spf13_cobra//:cobra",
1617
],

cmd/i18nLanguage/i18nLanguage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const (
1212
hlURI = "i18n://hl"
1313
hlName = "all i18n host languages"
1414
hlDesc = "List all i18n host languages for YouTube regions"
15-
langURI = "i18n://language"
16-
langName = "all i18n languages"
15+
langURI = "i18n://language/{hl}"
16+
langName = "i18n languages"
1717
)
1818

1919
var (

cmd/i18nLanguage/list.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77

88
"github.com/eat-pray-ai/yutu/cmd"
99
"github.com/eat-pray-ai/yutu/pkg/i18nLanguage"
10+
"github.com/eat-pray-ai/yutu/pkg/utils"
1011
"github.com/mark3labs/mcp-go/mcp"
1112
"github.com/spf13/cobra"
1213
)
1314

1415
func init() {
15-
// cmd.MCP.AddTool(listTool, listHandler)
16-
cmd.MCP.AddResource(langsResource, langsHandler)
1716
cmd.MCP.AddResource(hlResource, hlHandler)
17+
cmd.MCP.AddResourceTemplate(langsResource, langsHandler)
1818
i18nLanguageCmd.AddCommand(listCmd)
1919
listCmd.Flags().StringVarP(&hl, "hl", "l", "", hlUsage)
2020
listCmd.Flags().StringSliceVarP(
@@ -41,6 +41,7 @@ var hlResource = mcp.NewResource(
4141
hlURI, hlName,
4242
mcp.WithMIMEType(cmd.JsonMIME),
4343
mcp.WithResourceDescription(hlDesc),
44+
mcp.WithAnnotations([]mcp.Role{"user", "assistant"}, 0.51),
4445
)
4546

4647
func hlHandler(
@@ -65,16 +66,18 @@ func hlHandler(
6566
return contents, nil
6667
}
6768

68-
var langsResource = mcp.NewResource(
69+
var langsResource = mcp.NewResourceTemplate(
6970
langURI, langName,
70-
mcp.WithMIMEType(cmd.JsonMIME),
71-
mcp.WithResourceDescription(long),
71+
mcp.WithTemplateMIMEType(cmd.JsonMIME),
72+
mcp.WithTemplateDescription(long),
73+
mcp.WithTemplateAnnotations([]mcp.Role{"user", "assistant"}, 0.51),
7274
)
7375

7476
func langsHandler(
7577
ctx context.Context, request mcp.ReadResourceRequest,
7678
) ([]mcp.ResourceContents, error) {
7779
parts = defaultParts
80+
hl = utils.ExtractHl(request.Params.URI)
7881
output = "json"
7982
var writer bytes.Buffer
8083
err := list(&writer)
@@ -84,7 +87,7 @@ func langsHandler(
8487

8588
contents := []mcp.ResourceContents{
8689
mcp.TextResourceContents{
87-
URI: langURI,
90+
URI: request.Params.URI,
8891
MIMEType: cmd.JsonMIME,
8992
Text: writer.String(),
9093
},

cmd/i18nRegion/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ go_library(
1111
deps = [
1212
"//cmd",
1313
"//pkg/i18nRegion",
14+
"//pkg/utils",
1415
"@com_github_mark3labs_mcp_go//mcp",
1516
"@com_github_spf13_cobra//:cobra",
1617
],

cmd/i18nRegion/i18nRegion.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const (
99
short = "List YouTube i18n regions"
1010
long = "List YouTube i18n regions' id, hl, and name"
1111
hlUsage = "Host language"
12-
regionURI = "i18n://region"
13-
regionName = "all i18n regions"
12+
regionURI = "i18n://region/{hl}"
13+
regionName = "i18n regions"
1414
)
1515

1616
var (

cmd/i18nRegion/list.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import (
77

88
"github.com/eat-pray-ai/yutu/cmd"
99
"github.com/eat-pray-ai/yutu/pkg/i18nRegion"
10+
"github.com/eat-pray-ai/yutu/pkg/utils"
1011
"github.com/mark3labs/mcp-go/mcp"
1112
"github.com/spf13/cobra"
1213
)
1314

1415
func init() {
15-
// cmd.MCP.AddTool(listTool, listHandler)
16-
cmd.MCP.AddResource(RegionsResource, regionsHandler)
16+
cmd.MCP.AddResourceTemplate(RegionsResource, regionsHandler)
1717
i18nRegionCmd.AddCommand(listCmd)
1818
listCmd.Flags().StringVarP(&hl, "hl", "l", "", hlUsage)
1919
listCmd.Flags().StringSliceVarP(
@@ -36,16 +36,18 @@ var listCmd = &cobra.Command{
3636
},
3737
}
3838

39-
var RegionsResource = mcp.NewResource(
39+
var RegionsResource = mcp.NewResourceTemplate(
4040
regionURI, regionName,
41-
mcp.WithMIMEType(cmd.JsonMIME),
42-
mcp.WithResourceDescription(long),
41+
mcp.WithTemplateMIMEType(cmd.JsonMIME),
42+
mcp.WithTemplateDescription(long),
43+
mcp.WithTemplateAnnotations([]mcp.Role{"user", "assistant"}, 0.51),
4344
)
4445

4546
func regionsHandler(
4647
ctx context.Context, request mcp.ReadResourceRequest,
4748
) ([]mcp.ResourceContents, error) {
4849
parts = defaultParts
50+
hl = utils.ExtractHl(request.Params.URI)
4951
output = "json"
5052
var writer bytes.Buffer
5153
err := list(&writer)
@@ -55,7 +57,7 @@ func regionsHandler(
5557

5658
contents := []mcp.ResourceContents{
5759
mcp.TextResourceContents{
58-
URI: regionURI,
60+
URI: request.Params.URI,
5961
MIMEType: cmd.JsonMIME,
6062
Text: writer.String(),
6163
},

pkg/utils/utils.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import (
55
"encoding/base64"
66
"encoding/json"
77
"fmt"
8-
"github.com/ohler55/ojg/jp"
9-
"github.com/spf13/pflag"
108
"io"
119
"os/exec"
1210
"path/filepath"
11+
"regexp"
1312
"runtime"
1413

14+
"github.com/ohler55/ojg/jp"
15+
"github.com/spf13/pflag"
16+
1517
"gopkg.in/yaml.v3"
1618
)
1719

@@ -89,3 +91,12 @@ func ResetBool(m map[string]**bool, flagSet *pflag.FlagSet) {
8991
}
9092
}
9193
}
94+
95+
func ExtractHl(uri string) string {
96+
pattern := `i18n://(?:language|region)/([^/]+)`
97+
matches := regexp.MustCompile(pattern).FindStringSubmatch(uri)
98+
if len(matches) > 1 {
99+
return matches[1]
100+
}
101+
return ""
102+
}

pkg/utils/utils_test.go

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package utils
22

33
import (
44
"bytes"
5-
"github.com/spf13/cobra"
6-
"github.com/spf13/pflag"
75
"reflect"
86
"testing"
7+
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/pflag"
910
)
1011

1112
func TestBoolPtr(t *testing.T) {
@@ -305,3 +306,54 @@ func TestResetBool(t *testing.T) {
305306
)
306307
}
307308
}
309+
310+
func TestExtractHl(t *testing.T) {
311+
type args struct {
312+
uri string
313+
}
314+
tests := []struct {
315+
name string
316+
args args
317+
want string
318+
}{
319+
{
320+
name: "valid language uri with hl",
321+
args: args{uri: "i18n://language/zh-CN"},
322+
want: "zh-CN",
323+
},
324+
{
325+
name: "valid region uri with hl",
326+
args: args{uri: "i18n://region/zh-CN"},
327+
want: "zh-CN",
328+
},
329+
{
330+
name: "valid language uri without hl",
331+
args: args{uri: "i18n://language/"},
332+
want: "",
333+
},
334+
{
335+
name: "valid region uri without hl",
336+
args: args{uri: "i18n://region/"},
337+
want: "",
338+
},
339+
{
340+
name: "invalid uri",
341+
args: args{uri: "i18n://invalid/zh-CN"},
342+
want: "",
343+
},
344+
{
345+
name: "empty uri",
346+
args: args{uri: ""},
347+
want: "",
348+
},
349+
}
350+
for _, tt := range tests {
351+
t.Run(
352+
tt.name, func(t *testing.T) {
353+
if got := ExtractHl(tt.args.uri); got != tt.want {
354+
t.Errorf("ExtractHl() = %v, want %v", got, tt.want)
355+
}
356+
},
357+
)
358+
}
359+
}

0 commit comments

Comments
 (0)