Python client for Nacos.
Project description
nacos-sdk-python v3
English | 简体中文
A Python implementation of Nacos OpenAPI.
see: https://nacos.io/zh-cn/docs/open-API.html
Supported Python version:
Python 3.10+
Supported Nacos version
Supported Nacos version over 3.x
Note: AI Client feature requires Nacos server version 3.1.0 or above.
Installation
pip install nacos-sdk-python
Client Configuration
from v2.nacos import NacosNamingService, NacosConfigService, NacosAIService, ClientConfigBuilder, GRPCConfig, \
Instance, SubscribeServiceParam, RegisterInstanceParam, DeregisterInstanceParam, \
BatchRegisterInstanceParam, GetServiceParam, ListServiceParam, ListInstanceParam, ConfigParam
client_config = (ClientConfigBuilder()
.access_key(os.getenv('NACOS_ACCESS_KEY'))
.secret_key(os.getenv('NACOS_SECRET_KEY'))
.server_address(os.getenv('NACOS_SERVER_ADDR', 'localhost:8848'))
.log_level('INFO')
.grpc_config(GRPCConfig(grpc_timeout=5000))
.build())
- server_address - required - Nacos server address
- access_key - The aliyun accessKey to authenticate.
- secret_key - The aliyun secretKey to authenticate.
- credentials_provider - The custom access key manager.
- username - The username to authenticate.
- password - The password to authenticate.
- log_level - Log level | default:
logging.INFO - cache_dir - cache dir path. | default:
~/nacos/cache - log_dir - log dir path. | default:
~/logs/nacos - namespace_id - namespace id. | default: ``
- grpc_config - grpc config.
- max_receive_message_length - max receive message length in grpc. | default: 100 * 1024 * 1024
- max_keep_alive_ms - max keep alive ms in grpc. | default: 60 * 1000
- initial_window_size - initial window size in grpc. | default: 10 * 1024 * 1024
- initial_conn_window_size - initial connection window size in grpc. | default: 10 * 1024 * 1024
- grpc_timeout - grpc timeout in milliseconds. | default: 3000
- port_offset - gRPC port offset. The gRPC port = HTTP port + offset. | default: 1000
- capability_negotiation_timeout - timeout for capability negotiation in milliseconds. | default: 5000
- tls_config - tls config
- enabled - whether enable tls.
- ca_file - ca file path.
- cert_file - cert file path.
- key_file - key file path.
- kms_config - aliyun kms config
- enabled - whether enable aliyun kms.
- endpoint - aliyun kms endpoint.
- access_key - aliyun accessKey.
- secret_key - aliyun secretKey.
- password - aliyun kms password.
Config Client
config_client = await NacosConfigService.create_config_service(client_config)
config client common parameters
param: ConfigParam
paramdata_id Data id.paramgroup Group, useDEFAULT_GROUPif no group specified.paramcontent Config content.paramtag Config tag.paramapp_name Application name.parambeta_ips Beta test ip address.paramcas_md5 MD5 check code.paramtype Config type.paramsrc_user Source user.paramencrypted_data_key Encrypted data key.paramkms_key_id Kms encrypted data key id.paramusage_type Usage type.
Get Config
content = await config_client.get_config(ConfigParam(
data_id=data_id,
group=group
))
paramConfigParam config client common parameters. When getting configuration, it is necessary to specify the required data_id and group in param.returnConfig content if success or an exception will be raised.
Get value of one config item following priority:
-
Step 1 - Get from local failover dir.
-
Step 2 - Get from one server until value is got or all servers tried.
- Content will be saved to snapshot dir after got from server.
-
Step 3 - Get from snapshot dir.
Add Listener
async def config_listener(tenant, data_id, group, content):
print("listen, tenant:{} data_id:{} group:{} content:{}".format(tenant, data_id, group, content))
await config_client.add_listener(dataID, groupName, config_listener)
paramConfigParam config client common parameters.listenerlistener Configure listener, defined by the namespace_id、group、data_id、content.return
Add Listener to a specified config item.
- Once changes or deletion of the item happened, callback functions will be invoked.
- If the item is already exists in server, callback functions will be invoked for once.
- Callback functions are invoked from current process.
Remove Listener
await client.remove_listener(dataID, groupName, config_listener)
paramConfigParam config client common parameters.returnTrue if success or an exception will be raised.
Remove watcher from specified key.
Publish Config
res = await client.publish_config(ConfigParam(
data_id=dataID,
group=groupName,
content="Hello world")
)
paramConfigParam config client common parameters. When publishing configuration, it is necessary to specify the required data_id, group and content in param.returnTrue if success or an exception will be raised.
Publish one congfig data item to Nacos.
- If the data key is not exist, create one first.
- If the data key is exist, update to the content specified.
- Content can not be set to None, if there is need to delete config item, use function remove instead.
Remove Config
res = await client.remove_config(ConfigParam(
data_id=dataID,
group=groupName
))
paramConfigParam config client common parameters.When removing configuration, it is necessary to specify the required data_id and group in param.returnTrue if success or an exception will be raised.
Remove one config data item from Nacos.
Stop Config Client
await client.shutdown()
Naming Client
naming_client = await NacosNamingService.create_naming_service(client_config)
Register Instance
response = await client.register_instance(
request=RegisterInstanceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', ip='1.1.1.1',
port=7001, weight=1.0, cluster_name='c1', metadata={'a': 'b'},
enabled=True,
healthy=True, ephemeral=True))
Batch Register Instance
param1 = RegisterInstanceParam(service_name='nacos.test.1',
group_name='DEFAULT_GROUP',
ip='1.1.1.1',
port=7001,
weight=1.0,
cluster_name='c1',
metadata={'a': 'b'},
enabled=True,
healthy=True,
ephemeral=True
)
param2 = RegisterInstanceParam(service_name='nacos.test.1',
group_name='DEFAULT_GROUP',
ip='1.1.1.1',
port=7002,
weight=1.0,
cluster_name='c1',
metadata={'a': 'b'},
enabled=True,
healthy=True,
ephemeral=True
)
param3 = RegisterInstanceParam(service_name='nacos.test.1',
group_name='DEFAULT_GROUP',
ip='1.1.1.1',
port=7003,
weight=1.0,
cluster_name='c1',
metadata={'a': 'b'},
enabled=True,
healthy=False,
ephemeral=True
)
response = await client.batch_register_instances(
request=BatchRegisterInstanceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP',
instances=[param1, param2, param3]))
Deregister Instance
response = await client.deregister_instance(
request=DeregisterInstanceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', ip='1.1.1.1',
port=7001, cluster_name='c1', ephemeral=True)
)
Update Instance
response = await client.update_instance(
request=RegisterInstanceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', ip='1.1.1.1',
port=7001, weight=2.0, cluster_name='c1', metadata={'a': 'b'},
enabled=True,
healthy=True, ephemeral=True))
Get Service
service = await client.get_service(
GetServiceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', cluster_name='c1'))
List Service
service_list = await client.list_services(ListServiceParam())
List Instance
instance_list = await client.list_instances(ListInstanceParam(service_name='nacos.test.1', healthy_only=True))
instance_list = await client.list_instances(ListInstanceParam(service_name='nacos.test.1', healthy_only=False))
instance_list = await client.list_instances(ListInstanceParam(service_name='nacos.test.1', healthy_only=None))
Subscribe
async def cb(instance_list: List[Instance]):
print('received subscribe callback', str(instance_list))
await client.subscribe(
SubscribeServiceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', subscribe_callback=cb))
Unsubscribe
async def cb(instance_list: List[Instance]):
print('received subscribe callback', str(instance_list))
await client.unsubscribe(
SubscribeServiceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', subscribe_callback=cb))
Stop Naming Client
await client.shutdown()
AI Client
Important: AI Client feature requires Nacos server version 3.1.0 or above.
from v2.nacos import NacosAIService, ClientConfigBuilder
client_config = (ClientConfigBuilder()
.server_address(os.getenv('NACOS_SERVER_ADDR', 'localhost:8848'))
.build())
ai_client = await NacosAIService.create_ai_service(client_config)
MCP Server Management
Nacos provides management capabilities for MCP (Model Context Protocol) Server, including registration, discovery, and subscription, supporting dynamic registration and service discovery of MCP servers.
Get MCP Server
from v2.nacos.ai.model.ai_param import GetMcpServerParam
mcp_server = await ai_client.get_mcp_server(
GetMcpServerParam(mcp_name='my-mcp-server', version='1.0.0')
)
paramGetMcpServerParam Parameter for retrieving MCP server information.mcp_name- Name of the MCP server to query (required).version- Version of the MCP server to query (optional).
returnMcpServerDetailInfo if success or an exception will be raised.
Release MCP Server
from v2.nacos.ai.model.ai_param import ReleaseMcpServerParam
from v2.nacos.ai.model.mcp.mcp import McpServerBasicInfo, ServerVersionDetail
server_spec = McpServerBasicInfo(
name='my-mcp-server',
description='My MCP Server',
protocol='http',
versionDetail=ServerVersionDetail(version='1.0.0')
)
result = await ai_client.release_mcp_server(
ReleaseMcpServerParam(server_spec=server_spec)
)
paramReleaseMcpServerParam Parameter for releasing/publishing MCP server.server_spec- Basic information specification for the MCP server (required).tool_spec- Tool specification defining the tools provided by MCP server (optional).mcp_endpoint_spec- Endpoint specification for MCP server network configuration (optional).
returnServer ID if success or an exception will be raised.
Register MCP Server Endpoint
from v2.nacos.ai.model.ai_param import RegisterMcpServerEndpointParam
await ai_client.register_mcp_server_endpoint(
RegisterMcpServerEndpointParam(
mcp_name='my-mcp-server',
address='127.0.0.1',
port=8080,
version='1.0.0'
)
)
paramRegisterMcpServerEndpointParam Parameter for registering MCP server endpoint.mcp_name- Name of the MCP server (required).address- IP address or hostname of the MCP server endpoint (required).port- Port number of the MCP server endpoint (required).version- Version of the MCP server (optional).
Subscribe MCP Server
from v2.nacos.ai.model.ai_param import SubscribeMcpServerParam
async def mcp_listener(mcp_id, namespace_id, mcp_name, mcp_server_detail):
print(f"MCP Server changed: {mcp_name}, version: {mcp_server_detail.version}")
await ai_client.subscribe_mcp_server(
SubscribeMcpServerParam(
mcp_name='my-mcp-server',
version='1.0.0',
subscribe_callback=mcp_listener
)
)
paramSubscribeMcpServerParam Parameter for subscribing to MCP server changes.mcp_name- Name of the MCP server to subscribe to (required).version- Version of the MCP server to subscribe to (optional).subscribe_callback- Callback function to handle MCP server changes (required).
Unsubscribe MCP Server
await ai_client.unsubscribe_mcp_server(
SubscribeMcpServerParam(
mcp_name='my-mcp-server',
version='1.0.0',
subscribe_callback=mcp_listener
)
)
Agent Card Management
Nacos provides management capabilities for AI Agent, including registration, discovery, and subscription, supporting dynamic registration and service discovery of Agent Card based on A2A protocol.
Get Agent Card
from v2.nacos.ai.model.ai_param import GetAgentCardParam
agent_card = await ai_client.get_agent_card(
GetAgentCardParam(agent_name='my-agent', version='1.0.0')
)
paramGetAgentCardParam Parameter for retrieving agent card information.agent_name- Name of the agent card (required).version- Target version, if null or empty, get latest version (optional).registration_type- Registration type: 'url' or 'service' (optional).
returnAgentCardDetailInfo if success or an exception will be raised.
Release Agent Card
from v2.nacos.ai.model.ai_param import ReleaseAgentCardParam
from a2a.types import AgentCard
agent_card = AgentCard(
name='my-agent',
version='1.0.0',
protocol_version='1.0'
)
await ai_client.release_agent_card(
ReleaseAgentCardParam(
agent_card=agent_card,
registration_type='service',
set_as_latest=True
)
)
paramReleaseAgentCardParam Parameter for releasing/publishing agent card.agent_card- Agent card information (required).registration_type- Registration type: 'url' or 'service' (optional, default: 'service').set_as_latest- Whether to set as the latest version (optional, default: False).
Register Agent Endpoint
from v2.nacos.ai.model.ai_param import RegisterAgentEndpointParam
await ai_client.register_agent_endpoint(
RegisterAgentEndpointParam(
agent_name='my-agent',
address='127.0.0.1',
port=8080,
version='1.0.0',
transport='JSONRPC',
path='/agent',
support_tls=False
)
)
paramRegisterAgentEndpointParam Parameter for registering agent endpoint.agent_name- Name of the agent (required).address- IP address or hostname of the agent endpoint (required).port- Port number of the agent endpoint (required).version- Version of the agent (required).transport- Transport protocol (optional, default: 'JSONRPC').path- URL path for the endpoint (optional).support_tls- Whether TLS is supported (optional, default: False).
Deregister Agent Endpoint
from v2.nacos.ai.model.ai_param import DeregisterAgentEndpointParam
await ai_client.deregister_agent_endpoint(
DeregisterAgentEndpointParam(
agent_name='my-agent',
address='127.0.0.1',
port=8080,
version='1.0.0'
)
)
paramDeregisterAgentEndpointParam Parameter for deregistering agent endpoint.agent_name- Name of the agent (required).address- IP address or hostname of the agent endpoint (required).port- Port number of the agent endpoint (required).version- Version of the agent (required).
Subscribe Agent Card
from v2.nacos.ai.model.ai_param import SubscribeAgentCardParam
async def agent_listener(agent_name, agent_card_detail):
print(f"Agent card changed: {agent_name}, version: {agent_card_detail.version}")
await ai_client.subscribe_agent_card(
SubscribeAgentCardParam(
agent_name='my-agent',
version='1.0.0',
subscribe_callback=agent_listener
)
)
paramSubscribeAgentCardParam Parameter for subscribing to agent card changes.agent_name- Name of the agent (required).version- Version of the agent (optional).subscribe_callback- Callback function to handle agent card changes (required).
Unsubscribe Agent Card
await ai_client.unsubscribe_agent_card(
SubscribeAgentCardParam(
agent_name='my-agent',
version='1.0.0',
subscribe_callback=agent_listener
)
)
Stop AI Client
await ai_client.shutdown()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nacos_sdk_python-3.0.4.tar.gz.
File metadata
- Download URL: nacos_sdk_python-3.0.4.tar.gz
- Upload date:
- Size: 79.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3ac364dbeed4d5f5f7577859a2f0f5c6890626d424cb5ffef2efe4d5ab72c21
|
|
| MD5 |
22bcb64ae96d73dfa32f8dce8d45dad1
|
|
| BLAKE2b-256 |
79ff54866f457714838962edd098924d532a226b6523eacaf7c3335ef25ff413
|
File details
Details for the file nacos_sdk_python-3.0.4-py3-none-any.whl.
File metadata
- Download URL: nacos_sdk_python-3.0.4-py3-none-any.whl
- Upload date:
- Size: 109.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59e241a4a9aa6f1d856128671ee6e31aff1505d53cce2175eca1bd2e1253f283
|
|
| MD5 |
bb6e0f1739345148438e3da968f745d8
|
|
| BLAKE2b-256 |
9cb39247387bdfe34444e631141015f3b9664b77c1daa624a478aba9ba4ae398
|