Skip to content

feat: mcp supports #1050

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

Merged
merged 5 commits into from
Apr 21, 2025
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ coverage.txt
*.o
build/
.env
example/10-ai/*.yaml
zipper.yml
*.md
61 changes: 61 additions & 0 deletions ai/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ai

import (
"sync"

"github.com/sashabaranov/go-openai"
"github.com/yomorun/yomo/core/metadata"
)

var (
mu sync.Mutex
defaultRegister Register
)

// SetRegister sets the default register
func SetRegister(r Register) {
mu.Lock()
defer mu.Unlock()
defaultRegister = r

Check warning on line 19 in ai/register.go

View check run for this annotation

Codecov / codecov/patch

ai/register.go#L16-L19

Added lines #L16 - L19 were not covered by tests
}

// GetRegister gets the default register
func GetRegister() Register {
mu.Lock()
defer mu.Unlock()
return defaultRegister

Check warning on line 26 in ai/register.go

View check run for this annotation

Codecov / codecov/patch

ai/register.go#L23-L26

Added lines #L23 - L26 were not covered by tests
}

// ListToolCalls returns the list of tool calls
func ListToolCalls(md metadata.M) ([]openai.Tool, error) {
if defaultRegister == nil {
return nil, nil
}
return defaultRegister.ListToolCalls(md)

Check warning on line 34 in ai/register.go

View check run for this annotation

Codecov / codecov/patch

ai/register.go#L30-L34

Added lines #L30 - L34 were not covered by tests
}

// RegisterFunction registers a function calling function
func RegisterFunction(functionDefinition *openai.FunctionDefinition, connID uint64, md metadata.M) error {
if defaultRegister == nil {
return nil
}
return defaultRegister.RegisterFunction(functionDefinition, connID, md)

Check warning on line 42 in ai/register.go

View check run for this annotation

Codecov / codecov/patch

ai/register.go#L38-L42

Added lines #L38 - L42 were not covered by tests
}

// UnregisterFunction unregisters a function calling function
func UnregisterFunction(connID uint64, md metadata.M) {
if defaultRegister == nil {
return
}
defaultRegister.UnregisterFunction(connID, md)

Check warning on line 50 in ai/register.go

View check run for this annotation

Codecov / codecov/patch

ai/register.go#L46-L50

Added lines #L46 - L50 were not covered by tests
}

// Register provides an stateful register for registering and unregistering functions
type Register interface {
// ListToolCalls returns the list of tool calls
ListToolCalls(md metadata.M) ([]openai.Tool, error)
// RegisterFunction registers a function calling function
RegisterFunction(fd *openai.FunctionDefinition, connID uint64, md metadata.M) error
// UnregisterFunction unregisters a function calling function
UnregisterFunction(connID uint64, md metadata.M)
}
27 changes: 25 additions & 2 deletions cli/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
"github.com/yomorun/yomo/pkg/bridge/ai/provider/vertexai"
"github.com/yomorun/yomo/pkg/bridge/ai/provider/vllm"
"github.com/yomorun/yomo/pkg/bridge/ai/provider/xai"
"github.com/yomorun/yomo/pkg/bridge/llm"
"github.com/yomorun/yomo/pkg/bridge/mcp"
)

// serveCmd represents the serve command
Expand Down Expand Up @@ -99,6 +101,16 @@
// add AI connection middleware
options = append(options, yomo.WithFrameListener(listener))
}
// check and parse the mcp server config
mcpConfig, err := mcp.ParseConfig(bridgeConf)
if err != nil {
if err == mcp.ErrConfigNotFound {
ylog.Warn("mcp server is disabled")
} else {
log.FailureStatusEvent(os.Stdout, "%s", err.Error())
return
}

Check warning on line 112 in cli/serve.go

View check run for this annotation

Codecov / codecov/patch

cli/serve.go#L105-L112

Added lines #L105 - L112 were not covered by tests
}
// new zipper
zipper, err := yomo.NewZipper(
conf.Name,
Expand All @@ -110,8 +122,8 @@
}
zipper.Logger().Info("using config file", "file_path", config)

// AI Server
if aiConfig != nil {
// AI Server

Check warning on line 126 in cli/serve.go

View check run for this annotation

Codecov / codecov/patch

cli/serve.go#L126

Added line #L126 was not covered by tests
// register the llm provider
registerAIProvider(aiConfig)
// start the llm api server
Expand All @@ -122,12 +134,23 @@
conn2, _ := listener.Dial()
reducer := ai.NewReducer(conn2, auth.NewCredential(fmt.Sprintf("token:%s", tokenString)))

err := ai.Serve(aiConfig, ylog.Default(), source, reducer)
err := llm.Serve(aiConfig, ylog.Default(), source, reducer)

Check warning on line 137 in cli/serve.go

View check run for this annotation

Codecov / codecov/patch

cli/serve.go#L137

Added line #L137 was not covered by tests
if err != nil {
log.FailureStatusEvent(os.Stdout, "%s", err.Error())
return
}
}()
// MCP Server
if mcpConfig != nil {
defer mcp.Stop()
go func() {
err = mcp.Start(mcpConfig, aiConfig, listenAddr, ylog.Default())
if err != nil {
log.FailureStatusEvent(os.Stdout, "%s", err.Error())
return
}

Check warning on line 151 in cli/serve.go

View check run for this annotation

Codecov / codecov/patch

cli/serve.go#L144-L151

Added lines #L144 - L151 were not covered by tests
}()
}
}

// start the zipper
Expand Down
6 changes: 3 additions & 3 deletions core/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

// authentication implements, Currently, only token authentication is implemented
_ "github.com/yomorun/yomo/pkg/auth"
"github.com/yomorun/yomo/pkg/bridge/ai/register"

"github.com/yomorun/yomo/pkg/frame-codec/y3codec"
yquic "github.com/yomorun/yomo/pkg/listener/quic"
pkgtls "github.com/yomorun/yomo/pkg/tls"
Expand Down Expand Up @@ -187,7 +187,7 @@

if conn.ClientType() == ClientTypeStreamFunction {
s.router.Remove(conn.ID())
register.UnregisterFunction(conn.ID(), conn.Metadata())
ai.UnregisterFunction(conn.ID(), conn.Metadata())
}
_ = s.connector.Remove(conn.ID())
}
Expand Down Expand Up @@ -284,7 +284,7 @@
if err := json.Unmarshal([]byte(definition), &fd); err != nil {
return fmt.Errorf("unmarshal function definition error: %s", err.Error())
}
if err := register.RegisterFunction(&fd, conn.ID(), md); err != nil {
if err := ai.RegisterFunction(&fd, conn.ID(), md); err != nil {

Check warning on line 287 in core/server.go

View check run for this annotation

Codecov / codecov/patch

core/server.go#L287

Added line #L287 was not covered by tests
return err
}
s.logger.Info("register ai function success", "function_name", fd.Name, "definition", string(definition))
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/invopop/jsonschema v0.13.0
github.com/joho/godotenv v1.5.1
github.com/lmittmann/tint v1.0.7
github.com/mark3labs/mcp-go v0.20.1
github.com/matoous/go-nanoid/v2 v2.1.0
github.com/quic-go/quic-go v0.50.1
github.com/robfig/cron/v3 v3.0.1
Expand All @@ -25,8 +26,6 @@ require (
github.com/spf13/viper v1.20.0
github.com/stretchr/testify v1.10.0
github.com/tetratelabs/wazero v1.9.0
github.com/tidwall/gjson v1.18.0
github.com/tidwall/sjson v1.2.5
github.com/vmihailenco/msgpack/v5 v5.4.1
github.com/yomorun/y3 v1.0.5
go.opentelemetry.io/otel v1.35.0
Expand Down Expand Up @@ -83,12 +82,15 @@ require (
github.com/spf13/afero v1.14.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/tklauser/go-sysconf v0.3.15 // indirect
github.com/tklauser/numcpus v0.10.0 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0 // indirect
Expand Down
Loading