Prechádzať zdrojové kódy

fix(mcp): 修复服务注册时工具方法的判断逻辑

- 优化了 serverAddToolsByMethod 函数中的 McpOptions 获取逻辑
- 增加了对 Options 结构的空值检查- 修复了可能导致工具方法未正确注册的问题
dcsunny 4 mesiacov pred
rodič
commit
bc572a6395
1 zmenil súbory, kde vykonal 5 pridanie a 2 odobranie
  1. 5 2
      mcp/tools.go

+ 5 - 2
mcp/tools.go

@@ -40,14 +40,17 @@ func ServerAddTools(s *server.MCPServer, srv any, svcDesc grpc.ServiceDesc) erro
 	for j := 0; j < ser.Methods().Len(); j++ {
 		method := ser.Methods().Get(j)
 		t, h := serverAddToolsByMethod(serviceName, srv, method, handlerMap)
+		if t == nil || h == nil {
+			continue
+		}
 		s.AddTool(*t, h)
 	}
 	return nil
 }
 
 func serverAddToolsByMethod(serviceName string, srv any, method protoreflect.MethodDescriptor, handlerMap map[string]grpc.MethodDesc) (*mcp2.Tool, server.ToolHandlerFunc) {
-	methodMcpOpts, _ := proto.GetExtension(method.Options(), annotations2.E_Options).(*annotations2.McpOptions)
-	if methodMcpOpts == nil || !methodMcpOpts.Enabled {
+	methodMcpOpts, _ := proto.GetExtension(method.Options(), annotations2.E_Options).(*annotations2.Options)
+	if methodMcpOpts == nil || methodMcpOpts.McpOptions == nil || !methodMcpOpts.GetMcpOptions().Enabled {
 		return nil, nil
 	}
 	methodOperation, _ := proto.GetExtension(method.Options(), openapi_v3.E_Operation).(*openapi_v3.Operation)