11import hashlib
22import logging
33from functools import wraps
4- from typing import (
5- Any ,
6- Callable ,
7- Optional ,
8- TypeVar ,
9- cast ,
10- )
4+ from typing import Any , Callable , Optional , TypeVar , cast
115
126from alibabacloud_arms20190808 .client import Client as ArmsClient
7+ from alibabacloud_credentials .client import Client as CredClient
138from alibabacloud_sls20201230 .client import Client
149from alibabacloud_sls20201230 .client import Client as SLSClient
15- from alibabacloud_sls20201230 .models import (
16- CallAiToolsRequest ,
17- CallAiToolsResponse ,
18- IndexJsonKey ,
19- )
10+ from alibabacloud_sls20201230 .models import (CallAiToolsRequest ,
11+ CallAiToolsResponse , IndexJsonKey )
2012from alibabacloud_tea_openapi import models as open_api_models
2113from alibabacloud_tea_util import models as util_models
2214from mcp .server .fastmcp import Context
2618
2719logger = logging .getLogger (__name__ )
2820
29-
30- class SLSClientWrapper :
21+ class CredentialWrapper :
3122 """
32- A wrapper for aliyun client
23+ A wrapper for aliyun credentials
3324 """
3425
26+ access_key_id : str
27+ access_key_secret : str
28+
3529 def __init__ (self , access_key_id : str , access_key_secret : str ):
3630 self .access_key_id = access_key_id
3731 self .access_key_secret = access_key_secret
32+
33+
34+
35+ class SLSClientWrapper :
36+ """
37+ A wrapper for aliyun client
38+ """
39+
40+ def __init__ (self , credential : Optional [CredentialWrapper ] = None ):
41+ self .credential = credential
42+
3843
3944 def with_region (
4045 self , region : str = None , endpoint : Optional [str ] = None
4146 ) -> SLSClient :
42- config = open_api_models .Config (
43- access_key_id = self .access_key_id ,
44- access_key_secret = self .access_key_secret ,
45- )
47+ if self .credential :
48+ config = open_api_models .Config (
49+ access_key_id = self .credential .access_key_id ,
50+ access_key_secret = self .credential .access_key_secret ,
51+ )
52+ else :
53+ credentialsClient = CredClient ()
54+ config = open_api_models .Config (credential = credentialsClient )
4655 config .endpoint = f"{ region } .log.aliyuncs.com"
4756 return SLSClient (config )
4857
@@ -52,15 +61,18 @@ class ArmsClientWrapper:
5261 A wrapper for aliyun arms client
5362 """
5463
55- def __init__ (self , access_key_id : str , access_key_secret : str ):
56- self .access_key_id = access_key_id
57- self .access_key_secret = access_key_secret
64+ def __init__ (self , credential : Optional [CredentialWrapper ] = None ):
65+ self .credential = credential
5866
5967 def with_region (self , region : str , endpoint : Optional [str ] = None ) -> ArmsClient :
60- config = open_api_models .Config (
61- access_key_id = self .access_key_id ,
62- access_key_secret = self .access_key_secret ,
63- )
68+ if self .credential :
69+ config = open_api_models .Config (
70+ access_key_id = self .credential .access_key_id ,
71+ access_key_secret = self .credential .access_key_secret ,
72+ )
73+ else :
74+ credentialsClient = CredClient ()
75+ config = open_api_models .Config (credential = credentialsClient )
6476 config .endpoint = endpoint or f"arms.{ region } .aliyuncs.com"
6577 return ArmsClient (config )
6678
@@ -180,3 +192,4 @@ def text_to_sql(
180192 except Exception as e :
181193 logger .error (f"调用SLS AI工具失败: { str (e )} " )
182194 raise
195+
0 commit comments