mcp

package
v1.4.4-alpha1202-diff-... Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2025 License: AGPL-3.0 Imports: 38 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MCPCommand = &cli.Command{
	Name:  "mcp",
	Usage: MCPCommandUsage,
	Flags: []cli.Flag{
		cli.StringFlag{Name: "transport", Usage: "transport protocol, e.g. sse/stdio", Value: "stdio"},
		cli.StringFlag{Name: "host", Usage: "if transport is sse, listen host", Value: "localhost"},
		cli.IntFlag{Name: "port", Usage: "if transport is sse, listen port", Value: 11432},
		cli.StringFlag{Name: "t,tool", Usage: "enable tool sets, split by ','"},
		cli.StringFlag{Name: "dt,disable-tool", Usage: "disable tool sets, split by ','"},
		cli.StringFlag{Name: "r,resource", Usage: "enable resource sets, split by ','"},
		cli.StringFlag{Name: "dr,disable-resource", Usage: "disable resource sets, split by ','"},
		cli.StringSliceFlag{Name: "script", Usage: "add the dynamic Yak script as a tool to the MCP server"},
		cli.StringFlag{Name: "base-url", Usage: "if transport is sse, the base url of the MCP server"},
		cli.BoolFlag{Name: "enable-yak-aitool", Usage: "enable yak ai tool"},
	},
	Action: func(c *cli.Context) error {
		yakit.CallPostInitDatabase()

		var err error
		transport := c.String("transport")
		host := c.String("host")
		port := c.Int("port")
		tool, disableTool := c.String("tool"), c.String("disable-tool")
		script := c.StringSlice("script")
		baseURL := c.String("base-url")
		enableYakAITool := c.Bool("enable-yak-aitool")
		toolSets := lo.FilterMap(strings.Split(tool, ","), func(item string, _ int) (string, bool) {
			item = strings.TrimSpace(item)
			return item, item != ""
		})
		disableToolSets := lo.FilterMap(strings.Split(disableTool, ","), func(item string, _ int) (string, bool) {
			item = strings.TrimSpace(item)
			return item, item != ""
		})
		resource, disableResource := c.String("resource"), c.String("disable-resource")
		resourceSets := lo.FilterMap(strings.Split(resource, ","), func(item string, _ int) (string, bool) {
			item = strings.TrimSpace(item)
			return item, item != ""
		})
		disableResourceSets := lo.FilterMap(strings.Split(disableResource, ","), func(item string, _ int) (string, bool) {
			item = strings.TrimSpace(item)
			return item, item != ""
		})

		opts := make([]McpServerOption, 0, len(toolSets)+len(disableToolSets)+len(resourceSets)+len(disableResourceSets))
		for _, toolSet := range toolSets {
			opts = append(opts, WithEnableToolSet(toolSet))
		}
		for _, toolSet := range disableToolSets {
			opts = append(opts, WithDisableToolSet(toolSet))
		}
		for _, resourceSet := range resourceSets {
			opts = append(opts, WithEnableResourceSet(resourceSet))
		}
		for _, resourceSet := range disableResourceSets {
			opts = append(opts, WithDisableResourceSet(resourceSet))
		}
		if len(script) > 0 {
			opts = append(opts, WithDynamicScript(script))
		}

		if enableYakAITool {
			_, yakitTools, err := yakit.SearchAIYakToolWithPagination(consts.GetGormProfileDatabase(), "", false, &ypb.Paging{
				OrderBy: "updated_at",
				Order:   "desc",
				Limit:   200,
			})
			if err != nil {
				log.Errorf("failed to search yakit tools: %s", err)
			}

			tools := make([]*mcptool.Tool, 0, len(yakitTools))
			for _, aiTool := range yakitTools {
				tool := mcptool.NewTool(aiTool.Name)
				tool.Description = aiTool.Description
				dataMap := map[string]any{}
				err := json.Unmarshal([]byte(aiTool.Params), &dataMap)
				if err != nil {
					log.Errorf("unmarshal aiTool.Params failed: %v", err)
					continue
				}
				tool.InputSchema.FromMap(dataMap)
				tool.YakScript = aiTool.Content
				tools = append(tools, tool)
			}

			opts = append(opts, WithYakScriptTools(tools...))
		}

		s, err := NewMCPServer(opts...)
		if err != nil {
			return err
		}
		switch transport {
		case "stdio":
			log.SetLevel(log.FatalLevel)
			err = s.ServeStdio()
		case "sse":
			if port == 0 {
				port = utils.GetRandomAvailableTCPPort()
			}
			hostPort := utils.HostPort(host, port)
			if baseURL == "" {
				baseURL = fmt.Sprintf("http://%s", hostPort)
			}
			log.Infof("start to listen reverse(mcp) on: %s", hostPort)
			log.Infof("mcp server endpoint: %s", baseURL)
			err = s.ServeSSE(hostPort, baseURL)
		default:
			return utils.Errorf("invalid transport: %v", transport)
		}
		if err != nil {
			return err
		}

		return nil
	},
}
View Source
var MCPCommandUsage = `` /* 241-byte string literal not displayed */
View Source
var NewLocalClient func(locals ...bool) (YakClientInterface, error)

Functions

func AddGlobalResourceSet

func AddGlobalResourceSet(setName string, opts ...ResourceSetOption)

func AddGlobalToolSet

func AddGlobalToolSet(setName string, opts ...ToolSetOption)

func GlobalResourceSetList

func GlobalResourceSetList() []string

func GlobalToolSetList

func GlobalToolSetList() []string

func NewCommonCallToolResult

func NewCommonCallToolResult(data any) (*mcp.CallToolResult, error)

func RegisterNewLocalClient

func RegisterNewLocalClient(f func(locals ...bool) (YakClientInterface, error))

Types

type MCPServer

type MCPServer struct {
	// contains filtered or unexported fields
}

func NewMCPServer

func NewMCPServer(opts ...McpServerOption) (*MCPServer, error)

func (*MCPServer) Close

func (s *MCPServer) Close(ctxs ...context.Context)

func (*MCPServer) ServeSSE

func (s *MCPServer) ServeSSE(addr, baseURL string) (err error)

func (*MCPServer) ServeStdio

func (s *MCPServer) ServeStdio() (err error)

type MCPServerConfig

type MCPServerConfig struct {
	// contains filtered or unexported fields
}

func NewMCPServerConfig

func NewMCPServerConfig() *MCPServerConfig

func (*MCPServerConfig) ApplyConfig

func (cfg *MCPServerConfig) ApplyConfig(s *MCPServer)

type McpServerOption

type McpServerOption func(*MCPServerConfig) error

func WithDisableCVEToolSet

func WithDisableCVEToolSet() McpServerOption

func WithDisableCodecToolSet

func WithDisableCodecToolSet() McpServerOption

func WithDisableHTTPFlowToolSet

func WithDisableHTTPFlowToolSet() McpServerOption

func WithDisableHybridScanToolSet

func WithDisableHybridScanToolSet() McpServerOption

func WithDisablePayloadToolSet

func WithDisablePayloadToolSet() McpServerOption

func WithDisablePortScanToolSet

func WithDisablePortScanToolSet() McpServerOption

func WithDisableResource

func WithDisableResource(name string) McpServerOption

func WithDisableResourceSet

func WithDisableResourceSet(name string) McpServerOption

func WithDisableTool

func WithDisableTool(name string) McpServerOption

func WithDisableToolSet

func WithDisableToolSet(name string) McpServerOption

func WithDisableYakDocumentToolSet

func WithDisableYakDocumentToolSet() McpServerOption

func WithDisableYakScriptToolSet

func WithDisableYakScriptToolSet() McpServerOption

func WithDynamicScript

func WithDynamicScript(script []string) McpServerOption

func WithEnableCVEToolSet

func WithEnableCVEToolSet() McpServerOption

func WithEnableCodecToolSet

func WithEnableCodecToolSet() McpServerOption

func WithEnableHTTPFlowToolSet

func WithEnableHTTPFlowToolSet() McpServerOption

func WithEnableHybridScanToolSet

func WithEnableHybridScanToolSet() McpServerOption

func WithEnablePayloadToolSet

func WithEnablePayloadToolSet() McpServerOption

func WithEnablePortScanToolSet

func WithEnablePortScanToolSet() McpServerOption

func WithEnableResource

func WithEnableResource(name string) McpServerOption

func WithEnableResourceSet

func WithEnableResourceSet(name string) McpServerOption

func WithEnableTool

func WithEnableTool(name string) McpServerOption

func WithEnableToolSet

func WithEnableToolSet(name string) McpServerOption

func WithEnableYakDocumentToolSet

func WithEnableYakDocumentToolSet() McpServerOption

func WithEnableYakScriptToolSet

func WithEnableYakScriptToolSet() McpServerOption

func WithYakScriptTools

func WithYakScriptTools(tools ...*mcp.Tool) McpServerOption

type ResourceHandlerWrapperFunc

type ResourceHandlerWrapperFunc func(*MCPServer) server.ResourceHandlerFunc

type ResourceSet

type ResourceSet struct {
	Resources map[string]*ResourceWithHandler
}

type ResourceSetOption

type ResourceSetOption func(*ResourceSet)

func WithResource

func WithResource(resource *mcp.Resource, handler ResourceHandlerWrapperFunc) ResourceSetOption

func WithResourceTemplate

func WithResourceTemplate(resource *mcp.ResourceTemplate, handler ResourceHandlerWrapperFunc) ResourceSetOption

type ResourceWithHandler

type ResourceWithHandler struct {
	// contains filtered or unexported fields
}

type ToolHandlerWrapperFunc

type ToolHandlerWrapperFunc func(*MCPServer) server.ToolHandlerFunc

type ToolSet

type ToolSet struct {
	Tools map[string]*ToolWithHandler
}

type ToolSetOption

type ToolSetOption func(*ToolSet)

func WithTool

func WithTool(tool *mcp.Tool, handler ToolHandlerWrapperFunc) ToolSetOption

type ToolWithHandler

type ToolWithHandler struct {
	// contains filtered or unexported fields
}

type YakClientInterface

type YakClientInterface interface {
	ypb.YakClient
	GetProfileDatabase() *gorm.DB
}

Directories

Path Synopsis
mcp-go
client
Package client provides MCP (Model Control Protocol) client implementations.
Package client provides MCP (Model Control Protocol) client implementations.
mcp
Package mcp defines the core types and interfaces for the Model Control Protocol (MCP).
Package mcp defines the core types and interfaces for the Model Control Protocol (MCP).
server
Package server provides MCP (Model Control Protocol) server implementations.
Package server provides MCP (Model Control Protocol) server implementations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL