Skip to content

feat(go): add MCP plugin support for resources #3358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: jh-go-resources
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions go/core/resource/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package resource

import (
"fmt"
"strings"

"github.com/yosida95/uritemplate/v3"
)
Expand All @@ -29,6 +30,30 @@ type URIMatcher interface {
ExtractVariables(uri string) (map[string]string, error)
}

// normalizeURI cleans up a URI for template matching by removing common user issues:
// - Query parameters: "api://search/hello?limit=10" → "api://search/hello"
// - Trailing slashes: "api://users/123/" → "api://users/123"
// - URI fragments: "docs://page/intro#section1" → "docs://page/intro"
// This improves user experience by making template matching more forgiving.
func normalizeURI(uri string) string {
// Remove query parameters
if idx := strings.Index(uri, "?"); idx != -1 {
uri = uri[:idx]
}

// Remove URI fragments
if idx := strings.Index(uri, "#"); idx != -1 {
uri = uri[:idx]
}

// Remove trailing slash (but keep if it's just the root "/")
if len(uri) > 1 && strings.HasSuffix(uri, "/") {
uri = uri[:len(uri)-1]
}

return uri
}

// NewStaticMatcher creates a matcher for exact URI matches.
func NewStaticMatcher(uri string) URIMatcher {
return &staticMatcher{uri: uri}
Expand Down Expand Up @@ -65,12 +90,14 @@ type templateMatcher struct {
}

func (m *templateMatcher) Matches(uri string) bool {
values := m.template.Match(uri)
cleanURI := normalizeURI(uri)
values := m.template.Match(cleanURI)
return len(values) > 0
}

func (m *templateMatcher) ExtractVariables(uri string) (map[string]string, error) {
values := m.template.Match(uri)
cleanURI := normalizeURI(uri)
values := m.template.Match(cleanURI)
if len(values) == 0 {
return nil, fmt.Errorf("URI %q does not match template", uri)
}
Expand Down
17 changes: 17 additions & 0 deletions go/genkit/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,20 @@ func (d *detachedResourceAction) Register(r *registry.Registry) {
func (r *resourceAction) Name() string {
return r.action.Name()
}

// ListResources returns a slice of all resource actions
func ListResources(g *Genkit) []core.Action {
acts := g.reg.ListActions()
resources := []core.Action{}
for _, act := range acts {
action, ok := act.(core.Action)
if !ok {
continue
}
actionDesc := action.Desc()
if actionDesc.Type == core.ActionTypeResource {
resources = append(resources, action)
}
}
return resources
}
17 changes: 3 additions & 14 deletions go/plugins/mcp/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package mcp

import (
"fmt"
"strings"

"github.com/mark3labs/mcp-go/mcp"
)
Expand All @@ -32,19 +31,9 @@ func (c *GenkitMCPClient) GetToolNameWithNamespace(toolName string) string {
return fmt.Sprintf("%s_%s", c.options.Name, toolName)
}

// ContentToText extracts text content from MCP Content
func ContentToText(contentList []mcp.Content) string {
var textParts []string
for _, contentItem := range contentList {
if textContent, ok := contentItem.(mcp.TextContent); ok && textContent.Type == "text" {
textParts = append(textParts, textContent.Text)
} else if erContent, ok := contentItem.(mcp.EmbeddedResource); ok {
if trc, ok := erContent.Resource.(mcp.TextResourceContents); ok {
textParts = append(textParts, trc.Text)
}
}
}
return strings.Join(textParts, "")
// GetResourceNameWithNamespace returns a resource name prefixed with the client's namespace
func (c *GenkitMCPClient) GetResourceNameWithNamespace(resourceName string) string {
return fmt.Sprintf("%s_%s", c.options.Name, resourceName)
}

// ExtractTextFromContent extracts text content from MCP Content
Expand Down
38 changes: 38 additions & 0 deletions go/plugins/mcp/fixtures/basic_server/basic_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"

"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/core"
"github.com/firebase/genkit/go/genkit"
"github.com/firebase/genkit/go/plugins/mcp"
)

func main() {
g, _ := genkit.Init(context.Background())
genkit.DefineResource(g, genkit.ResourceOptions{
Name: "test-docs",
Template: "file://test/{filename}",
}, func(ctx context.Context, input core.ResourceInput) (genkit.ResourceOutput, error) {
return genkit.ResourceOutput{
Content: []*ai.Part{ai.NewTextPart("test content")},
}, nil
})
server := mcp.NewMCPServer(g, mcp.MCPServerOptions{Name: "test"})
server.ServeStdio()
}
44 changes: 44 additions & 0 deletions go/plugins/mcp/fixtures/content_server/content_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
"fmt"

"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/core"
"github.com/firebase/genkit/go/genkit"
"github.com/firebase/genkit/go/plugins/mcp"
)

func main() {
g, _ := genkit.Init(context.Background())

// Resource that provides different content based on filename
genkit.DefineResource(g, genkit.ResourceOptions{
Name: "content-provider",
Template: "file://data/{filename}",
}, func(ctx context.Context, input core.ResourceInput) (genkit.ResourceOutput, error) {
filename := input.Variables["filename"]
content := fmt.Sprintf("CONTENT_FROM_SERVER: This is %s with important data.", filename)
return genkit.ResourceOutput{
Content: []*ai.Part{ai.NewTextPart(content)},
}, nil
})

server := mcp.NewMCPServer(g, mcp.MCPServerOptions{Name: "content-server"})
server.ServeStdio()
}
38 changes: 38 additions & 0 deletions go/plugins/mcp/fixtures/policy_server/policy_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"

"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/core"
"github.com/firebase/genkit/go/genkit"
"github.com/firebase/genkit/go/plugins/mcp"
)

func main() {
g, _ := genkit.Init(context.Background())
genkit.DefineResource(g, genkit.ResourceOptions{
Name: "company-policy",
Template: "docs://policy/{section}",
}, func(ctx context.Context, input core.ResourceInput) (genkit.ResourceOutput, error) {
return genkit.ResourceOutput{
Content: []*ai.Part{ai.NewTextPart("VACATION_POLICY: Employees get 20 days vacation per year.")},
}, nil
})
server := mcp.NewMCPServer(g, mcp.MCPServerOptions{Name: "test"})
server.ServeStdio()
}
38 changes: 38 additions & 0 deletions go/plugins/mcp/fixtures/server_a/server_a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"

"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/core"
"github.com/firebase/genkit/go/genkit"
"github.com/firebase/genkit/go/plugins/mcp"
)

func main() {
g, _ := genkit.Init(context.Background())
genkit.DefineResource(g, genkit.ResourceOptions{
Name: "server-a-docs",
Template: "a://docs/{file}",
}, func(ctx context.Context, input core.ResourceInput) (genkit.ResourceOutput, error) {
return genkit.ResourceOutput{
Content: []*ai.Part{ai.NewTextPart("Content from Server A")},
}, nil
})
server := mcp.NewMCPServer(g, mcp.MCPServerOptions{Name: "server-a"})
server.ServeStdio()
}
38 changes: 38 additions & 0 deletions go/plugins/mcp/fixtures/server_b/server_b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"

"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/core"
"github.com/firebase/genkit/go/genkit"
"github.com/firebase/genkit/go/plugins/mcp"
)

func main() {
g, _ := genkit.Init(context.Background())
genkit.DefineResource(g, genkit.ResourceOptions{
Name: "server-b-files",
Template: "b://files/{path}",
}, func(ctx context.Context, input core.ResourceInput) (genkit.ResourceOutput, error) {
return genkit.ResourceOutput{
Content: []*ai.Part{ai.NewTextPart("Content from Server B")},
}, nil
})
server := mcp.NewMCPServer(g, mcp.MCPServerOptions{Name: "server-b"})
server.ServeStdio()
}
Loading
Loading