HANAMLToolkit

class hana_ai.tools.toolkit.HANAMLToolkit(connection_context, used_tools=None, return_direct=None, audit_enabled: bool = True, audit_log_path: str | None = None, audit_service_name: str = 'hana-ai-mcp-service', audit_environment: str = 'local', trust_proxy_headers: bool | None = None, trusted_proxy_cidrs: list[str] | str | None = None)

Toolkit for HANA data analysis and model workflows.

The separate SQL agent/toolkit is not included automatically. Its read-only checks do not apply to these tools: training, model storage and deletion may require write/execute privileges. Select tools and database roles accordingly.

Parameters:
connection_contextConnectionContext

Connection context to the HANA database.

used_toolslist, optional

List of tools to use. If None or 'all', all default tools are used. Default to None. Python execution is never included automatically; it requires a separately constructed, explicitly enabled trusted-code tool registered with add_custom_tool.

Examples

Assume cc is a connection to a SAP HANA instance:

>>> from hana_ai.tools.toolkit import HANAMLToolkit
>>> from hana_ai.agents.context_agent import ContextAgent
>>> tools = HANAMLToolkit(connection_context=cc, used_tools='all').get_tools()
>>> chatbot = ContextAgent(llm=llm, tools=tools, storage_dir='.context_agent', session_id='toolkit_example')
add_custom_tool(tool: BaseTool)

Add a custom tool to the toolkit.

Parameters:
toolBaseTool

Custom tool to add.

Note

The tool must be a subclass of BaseTool. Please follow the guide to create the custom tools https://python.langchain.com/docs/how_to/custom_tools/.

delete_tool(tool_name: str)

Delete a tool from the toolkit.

Parameters:
tool_namestr

Name of the tool to delete.

reset_tools(tools: List[BaseTool] | None = None)

Reset the toolkit's tools.

Parameters:
toolslist of BaseTool or list of str, optional

If provided, the toolkit will only contain these tools. When a list of strings is provided, tools will be matched by name from the default tools. If None, reset to default tools.

set_bas(bas=True)

Set the BAS mode for all tools in the toolkit.

set_vectordb(vectordb)

Set the vector database.

Parameters:
vectordbHANAMLinVectorEngine

Vector database.

is_port_available(port: int) → bool

检查端口是否可用

launch_mcp_server(server_name: str = 'HANATools', host: str = '127.0.0.1', transport: str = 'stdio', port: int = 8001, auth_token: str | None = None, max_retries: int = 5, stateless_http: bool = False)

Launch the MCP server with the specified configuration. This method initializes the MCP server, registers all tools, and starts the server in a background thread. If the specified port is occupied, it will try the next port up to max_retries times.

Parameters:
server_namestr

Name of the server. Default is "HANATools".

hoststr

Host address; defaults to 127.0.0.1. Non-loopback HTTP requires auth_token. SSE is restricted to loopback, even with a token.

transport{"stdio", "sse", "http"}

Transport protocol to use. Default is "stdio". Can be "sse" for Server-Sent Events.

portint

Network port to use for server transports that require a port (SSE/HTTP). Default is 8001. Ignored for stdio.

auth_tokenstr, optional

Bearer token for HTTP transport, required outside loopback and for HTTP connection administration. SSE rejects tokens and administration; stdio relies on the host process boundary. This launcher does not terminate TLS: remote deployments need an HTTPS reverse proxy and must prevent direct plaintext access.

max_retriesint

Maximum number of retries to find an available port. Default is 5.

stateless_httpbool

HTTP transport only. When True, run the Streamable HTTP server in stateless mode: no per-client mcp-session-id is issued or enforced, so clients that hold a previously-cached session id (e.g. Joule Desktop after a server restart) can reconnect without a handshake error. Every request is treated independently by the transport, but the audit layer still correlates them under a single process-lifetime session id so mcp.session.started is emitted exactly once per server start. Ignored for stdio / sse. Default is False (stateful — unchanged legacy behavior).

Notes

Connection administration is registered only when the server environment sets HANA_ALLOW_RUNTIME_CONN_UPDATE=true. HTTP administration also requires auth_token, including on loopback. Environment reload reads the trusted server environment; client-selected file reload is not supported. Connection updates default to TLS encryption and certificate validation; disabling either requires the Boolean allow_tls_downgrade=True.

get_tools() → List[BaseTool]

Get the tools in the toolkit.

stop_mcp_server(host: str = '127.0.0.1', port: int = 8001, transport: str = 'sse', force: bool = False, timeout: float = 5.0) → bool

停止指定地址与端口的 MCP 服务。

stop_all_mcp_servers(force: bool = False, timeout: float = 5.0) → int

关闭全部已注册 MCP 服务。