Browse Source

feat(mcp): 更新 mcp-go 依赖并添加 MCP 服务器实现

- 将 github.com/mark3labs/mcp-go 从 v0.30.1 升级到 v0.31.0
- 新增 MCP 服务器实现,包括 Server 结构体和相关方法
- 添加 ServerOption 类型和多个服务器配置函数
- 实现了 ServeHTTP、Endpoint、Start 和 Stop 方法
dcsunny 4 tháng trước cách đây
mục cha
commit
f618227652
3 tập tin đã thay đổi với 125 bổ sung1 xóa
  1. 1 1
      go.mod
  2. 2 0
      go.sum
  3. 122 0
      mcp/server.go

+ 1 - 1
go.mod

@@ -17,7 +17,7 @@ require (
 	github.com/google/uuid v1.6.0
 	github.com/jhump/protoreflect v1.17.0
 	github.com/lestrrat-go/jwx v1.2.25
-	github.com/mark3labs/mcp-go v0.30.1
+	github.com/mark3labs/mcp-go v0.31.0
 	github.com/vmihailenco/msgpack/v5 v5.3.0
 	google.golang.org/genproto v0.0.0-20231212172506-995d672761c0
 	google.golang.org/genproto/googleapis/api v0.0.0-20240102182953-50ed04b92917

+ 2 - 0
go.sum

@@ -867,6 +867,8 @@ github.com/lyft/protoc-gen-star v0.6.0/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuz
 github.com/lyft/protoc-gen-star v0.6.1/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA=
 github.com/mark3labs/mcp-go v0.30.1 h1:3R1BPvNT/rC1iPpLx+EMXFy+gvux/Mz/Nio3c6XEU9E=
 github.com/mark3labs/mcp-go v0.30.1/go.mod h1:rXqOudj/djTORU/ThxYx8fqEVj/5pvTuuebQ2RC7uk4=
+github.com/mark3labs/mcp-go v0.31.0 h1:4UxSV8aM770OPmTvaVe/b1rA2oZAjBMhGBfUgOGut+4=
+github.com/mark3labs/mcp-go v0.31.0/go.mod h1:rXqOudj/djTORU/ThxYx8fqEVj/5pvTuuebQ2RC7uk4=
 github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
 github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
 github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=

+ 122 - 0
mcp/server.go

@@ -0,0 +1,122 @@
+package mcp
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"net/http"
+	"net/url"
+
+	"github.com/go-kratos/kratos/v2/transport"
+
+	"github.com/mark3labs/mcp-go/server"
+)
+
+var (
+	_ transport.Server     = (*Server)(nil)
+	_ transport.Endpointer = (*Server)(nil)
+	_ http.Handler         = (*Server)(nil)
+)
+
+// MiddlewareFunc is a function that takes an http.Handler and returns an http.Handler.
+type MiddlewareFunc func(http.Handler) http.Handler
+
+// ServerOption is an HTTP server option.
+type ServerOption func(*Server)
+
+// Address with server address.
+func Address(addr string) ServerOption {
+	return func(s *Server) {
+		s.address = addr
+	}
+}
+
+// Endpoint with server address.
+func Endpoint(endpoint *url.URL) ServerOption {
+	return func(s *Server) {
+		s.endpoint = endpoint
+	}
+}
+
+// Middleware with server middleware.
+func Middleware(m MiddlewareFunc) ServerOption {
+	return func(s *Server) {
+		s.middleware = m
+	}
+}
+
+// SrvOptions with server options.
+func SrvOptions(opts ...server.ServerOption) ServerOption {
+	return func(s *Server) {
+		s.srvOpts = append(s.srvOpts, opts...)
+	}
+}
+
+func StreamableHTTPOptions(opts ...server.StreamableHTTPOption) ServerOption {
+	return func(s *Server) {
+		s.streamableHTTPOpts = append(s.streamableHTTPOpts, opts...)
+	}
+}
+
+// Server is a MCP server.
+type Server struct {
+	*server.MCPServer
+	srv                  *http.Server
+	streamableHttpServer *server.StreamableHTTPServer
+	middleware           MiddlewareFunc
+	address              string
+	endpoint             *url.URL
+	srvOpts              []server.ServerOption
+	streamableHTTPOpts   []server.StreamableHTTPOption
+	mux                  *http.ServeMux
+}
+
+// NewServer creates a new MCP server.
+func NewServer(name, version string, opts ...ServerOption) *Server {
+	srv := &Server{
+		middleware: func(next http.Handler) http.Handler { return next },
+	}
+	for _, o := range opts {
+		o(srv)
+	}
+	srv.MCPServer = server.NewMCPServer(name, version, srv.srvOpts...)
+	srv.srv = &http.Server{Handler: srv.middleware(srv)}
+	srv.streamableHttpServer = server.NewStreamableHTTPServer(srv.MCPServer, append(srv.streamableHTTPOpts, server.WithStreamableHTTPServer(srv.srv))...)
+	if srv.mux == nil {
+		srv.mux = http.NewServeMux()
+	}
+	srv.mux.Handle("/mcp", srv.streamableHttpServer)
+	srv.srv.Handler = srv.mux
+
+	return srv
+}
+
+// ServeHTTP implements the http.Handler interface.
+func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
+	s.streamableHttpServer.ServeHTTP(res, req)
+}
+
+// Endpoint return a real address to registry endpoint.
+// examples:
+// - http://127.0.0.1:8000
+func (s *Server) Endpoint() (*url.URL, error) {
+	if s.endpoint != nil {
+		return s.endpoint, nil
+	}
+	return url.Parse(fmt.Sprintf("http://%s", s.address))
+}
+
+// Start start the MCP server.
+func (s *Server) Start(_ context.Context) error {
+	if err := s.streamableHttpServer.Start(s.address); err != nil {
+		if !errors.Is(err, http.ErrServerClosed) {
+			return err
+		}
+	}
+	return nil
+}
+
+// Stop stop the MCP server.
+func (s *Server) Stop(ctx context.Context) error {
+	return s.streamableHttpServer.Shutdown(ctx)
+}