Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor agentplugin for better modularity
Signed-off-by: Jet Chiang <[email protected]>
  • Loading branch information
supreme-gg-gg committed Aug 31, 2026
commit b739599908ff2500a6f8c7f8ed47efb31a27d413
16 changes: 11 additions & 5 deletions go/adk/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ import (
runnerpkg "github.com/kagent-dev/kagent/go/adk/pkg/runner"
"github.com/kagent-dev/kagent/go/adk/pkg/session"
"github.com/kagent-dev/kagent/go/adk/pkg/telemetry"
"github.com/kagent-dev/kagent/go/core/v2/agentplugins"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

const (
defaultPluginPackagesRoot = "/plugins"
defaultSkillsRoot = "/skills"
defaultPluginDataRoot = "/data/plugins"
)

func setupLogger(logLevel string) (logr.Logger, *zap.Logger) {
var zapLevel zapcore.Level
switch strings.ToLower(logLevel) {
Expand Down Expand Up @@ -93,11 +98,12 @@ func main() {
logger.Error(err, "Failed to load agent config (model configuration is required)", "configDir", configDir)
os.Exit(1)
}
if err := agentplugins.MaterializeAgentConfig(
if err := config.MaterializeAgentPlugins(
logr.NewContext(context.Background(), logger), agentConfig,
agentplugins.ADKPaths{
SkillPaths: agentplugins.SkillPaths{Plugins: agentplugins.DefaultPluginRoot, Skills: agentplugins.DefaultSkillsRoot},
Data: agentplugins.DefaultDataRoot,
config.AgentPluginPaths{
Packages: defaultPluginPackagesRoot,
Skills: defaultSkillsRoot,
Data: defaultPluginDataRoot,
},
); err != nil {
logger.Error(err, "Failed to materialize Agent Plugins")
Expand Down
70 changes: 70 additions & 0 deletions go/adk/pkg/config/agentplugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package config

import (
"context"
"fmt"
"path/filepath"

"github.com/kagent-dev/kagent/go/api/adk"
"github.com/kagent-dev/kagent/go/core/v2/agentplugins"
)

// AgentPluginPaths contains the ADK runtime destinations for Agent Plugin
// packages, selected skills, and mutable MCP server data.
type AgentPluginPaths struct {
Packages string
Skills string
Data string
}

// MaterializeAgentPlugins materializes plugins independently for each agent
// and adds their skills and MCP servers to the ADK runtime configuration.
func MaterializeAgentPlugins(ctx context.Context, agentConfig *adk.AgentConfig, paths AgentPluginPaths) error {
if agentConfig.AgentPlugins != nil {
materialization, err := agentplugins.Materialize(ctx, *agentConfig.AgentPlugins, agentplugins.Paths{
Packages: paths.Packages,
Skills: paths.Skills,
})
if err != nil {
return fmt.Errorf("materialize agent plugins: %w", err)
}
mcpConfig, err := agentplugins.LoadMCP(ctx, materialization, paths.Data)
if err != nil {
return fmt.Errorf("load agent plugin MCP configuration: %w", err)
}
addMCPConfig(agentConfig, mcpConfig)
agentConfig.SkillsDirectory = materialization.SkillsDirectory
}
for i, child := range agentConfig.SubAgents {
childRoot := filepath.Join("subagents", fmt.Sprintf("%d", i))
if err := MaterializeAgentPlugins(ctx, child, AgentPluginPaths{
Packages: filepath.Join(paths.Packages, childRoot),
Skills: filepath.Join(paths.Skills, childRoot),
Data: filepath.Join(paths.Data, childRoot),
}); err != nil {
return fmt.Errorf("materialize sub-agent %q: %w", child.Name, err)
}
}
return nil
}

func addMCPConfig(agentConfig *adk.AgentConfig, mcpConfig agentplugins.MCPConfig) {
for _, server := range mcpConfig.StreamableHTTP {
agentConfig.HttpTools = append(agentConfig.HttpTools, adk.HttpMcpServerConfig{
Params: adk.StreamableHTTPConnectionParams{Url: server.URL, Headers: server.Headers},
})
}
for _, server := range mcpConfig.SSE {
agentConfig.SseTools = append(agentConfig.SseTools, adk.SseMcpServerConfig{
Params: adk.SseConnectionParams{Url: server.URL, Headers: server.Headers},
})
}
for _, server := range mcpConfig.Stdio {
agentConfig.StdioTools = append(agentConfig.StdioTools, adk.StdioMcpServerConfig{
Command: server.Command,
Args: server.Args,
Env: server.Env,
Dir: server.Dir,
})
}
}
71 changes: 71 additions & 0 deletions go/adk/pkg/config/agentplugins_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package config

import (
"context"
"os"
"path/filepath"
"strings"
"testing"

"github.com/kagent-dev/kagent/go/api/adk"
"github.com/kagent-dev/kagent/go/api/agentplugin"
"github.com/kagent-dev/kagent/go/core/v2/agentplugins"
)

func TestMaterializeAgentPluginsIsolatesSubagentSkills(t *testing.T) {
root := t.TempDir()
paths := AgentPluginPaths{
Packages: filepath.Join(root, "packages"),
Skills: filepath.Join(root, "skills"),
Data: filepath.Join(root, "data"),
}
source := agentplugin.Source{Git: &agentplugin.GitSource{URL: "unused", Commit: strings.Repeat("a", 40)}}
agentConfig := &adk.AgentConfig{
AgentPlugins: &agentplugin.Resources{Skills: []agentplugin.Skill{{Name: "root", Source: source}}},
SubAgents: []*adk.AgentConfig{{Name: "child", AgentPlugins: &agentplugin.Resources{Skills: []agentplugin.Skill{{Name: "child", Source: source}}}}},
}
for _, path := range []string{
filepath.Join(paths.Packages, "standalone-0"),
filepath.Join(paths.Packages, "subagents", "0", "standalone-0"),
} {
if err := os.MkdirAll(path, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(path, "SKILL.md"), []byte("# Skill"), 0o644); err != nil {
t.Fatal(err)
}
}
if err := MaterializeAgentPlugins(context.Background(), agentConfig, paths); err != nil {
t.Fatal(err)
}
if agentConfig.SkillsDirectory == agentConfig.SubAgents[0].SkillsDirectory {
t.Fatalf("root and child share skills directory %q", agentConfig.SkillsDirectory)
}
for _, path := range []string{
filepath.Join(agentConfig.SkillsDirectory, "root", "SKILL.md"),
filepath.Join(agentConfig.SubAgents[0].SkillsDirectory, "child", "SKILL.md"),
} {
if _, err := os.Stat(path); err != nil {
t.Fatalf("materialized skill %q: %v", path, err)
}
}
}

func TestAddMCPConfigConvertsRuntimeNeutralServers(t *testing.T) {
agentConfig := &adk.AgentConfig{}
addMCPConfig(agentConfig, agentplugins.MCPConfig{
StreamableHTTP: []agentplugins.RemoteMCPServer{{URL: "https://http.example.com", Headers: map[string]string{"X-Test": "http"}}},
SSE: []agentplugins.RemoteMCPServer{{URL: "https://sse.example.com", Headers: map[string]string{"X-Test": "sse"}}},
Stdio: []agentplugins.StdioMCPServer{{Command: "server", Args: []string{"--serve"}, Env: map[string]string{"KEY": "value"}, Dir: "/plugin"}},
})

if len(agentConfig.HttpTools) != 1 || agentConfig.HttpTools[0].Params.Url != "https://http.example.com" {
t.Fatalf("HTTP tools = %#v", agentConfig.HttpTools)
}
if len(agentConfig.SseTools) != 1 || agentConfig.SseTools[0].Params.Url != "https://sse.example.com" {
t.Fatalf("SSE tools = %#v", agentConfig.SseTools)
}
if len(agentConfig.StdioTools) != 1 || agentConfig.StdioTools[0].Command != "server" {
t.Fatalf("stdio tools = %#v", agentConfig.StdioTools)
}
}
Loading
Loading