-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.go
48 lines (39 loc) · 956 Bytes
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"context"
"github.com/go-rod/rod-mcp/tools"
"github.com/go-rod/rod-mcp/types"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
type Server struct {
ctx *types.Context
mcpServer *server.MCPServer
}
func NewServer(stdCtx context.Context, cfg types.Config) *Server {
ctx := types.NewContext(stdCtx, cfg)
mcpServer := server.NewMCPServer(cfg.ServerName, cfg.ServerVersion)
ser := &Server{
ctx: ctx,
mcpServer: mcpServer,
}
ser.registerTools(tools.CommonTools...)
return ser
}
func (s *Server) registerTools(mcpTools ...mcp.Tool) *Server {
for _, mt := range mcpTools {
if handlerFunc, ok := tools.CommonToolHandlers[mt.Name]; ok {
s.mcpServer.AddTool(mt, handlerFunc(s.ctx))
}
}
return s
}
func (s *Server) Start() error {
if err := server.ServeStdio(s.mcpServer); err != nil {
return err
}
return nil
}
func (s *Server) Close() error {
return s.ctx.Close()
}