Skip to content

Commit 3314d0f

Browse files
committed
增加stdio 调用模式
1 parent f08773f commit 3314d0f

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

main.go

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ func main() {
1919
// example.Example()
2020
// mcp.RunMCPServer("kom mcp server", "0.0.1", 9096)
2121

22+
// SSE调用时,可以在MCP client 请求中,在header中注入认证信息
23+
// kom执行时,如果注册了callback,那么可以在callback中获取到ctx,从ctx上可以拿到注入的认证信息
24+
// 有了认证信息,就可以在callback中,进行权限的逻辑控制了
2225
authKey := "username"
2326
authRoleKey := "role"
2427
var ctxFn = func(ctx context.Context, r *http.Request) context.Context {
@@ -44,6 +47,7 @@ func main() {
4447
},
4548
AuthKey: authKey,
4649
AuthRoleKey: authRoleKey,
50+
Mode: metadata.MCPServerModeBoth, // 同时开启STDIO 和 SSE
4751
}
4852
mcp.RunMCPServerWithOption(&cfg)
4953
}

mcp/metadata/type.go

+8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ type ServerConfig struct {
2222
Metadata map[string]string // 元数据
2323
AuthKey string // 认证key
2424
AuthRoleKey string // 认证key
25+
Mode MCPServerMode // 运行模式 sse,stdio
2526
}
27+
type MCPServerMode string
28+
29+
const (
30+
MCPServerModeSSE MCPServerMode = "sse"
31+
MCPServerModeStdio MCPServerMode = "stdio"
32+
MCPServerModeBoth MCPServerMode = "both"
33+
)
2634

2735
type ResourceInfo struct {
2836
Group string

mcp/server.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,20 @@ func RunMCPServerWithOption(cfg *metadata.ServerConfig) {
7878
storageclass.RegisterTools(s, config)
7979
ingressclass.RegisterTools(s, config)
8080
yaml.RegisterTools(s, config)
81+
82+
if cfg.Mode == metadata.MCPServerModeStdio || cfg.Mode == metadata.MCPServerModeBoth {
83+
// Start the stdio server
84+
if err := server.ServeStdio(s); err != nil {
85+
fmt.Printf("Server error: %v\n", err)
86+
}
87+
}
88+
8189
// 创建 SSE 服务器
8290
sseServer := server.NewSSEServer(s, config.SSEOption...)
83-
8491
// 启动服务器
8592
err := sseServer.Start(fmt.Sprintf(":%d", config.Port))
8693
if err != nil {
8794
klog.Errorf("MCP Server error: %v\n", err)
8895
}
96+
8997
}

0 commit comments

Comments
 (0)