McpToolRegistrationService 類別

提供與語意核心工具相關的服務。

此服務負責使用 Semantic Kernel 代理的 MCP(模型情境協定)工具伺服器的註冊與管理。

建構函式

McpToolRegistrationService()

參數

名稱 Description
logger
預設值: None

方法

__init__

初始化語意核心的 MCP 工具註冊服務。

__new__
add_tool_servers_to_agent

將 A365 MCP 工具伺服器加入指定核心。

cleanup_connections

清理所有連接的 MCP 插件。

send_chat_history

將語意核心的聊天紀錄傳送到 MCP 平台。

此方法從語意核心 ChatHistory 物件中擷取訊息,轉換為 ChatHistoryMessage 格式,並傳送至 MCP 平台以進行即時威脅防護。

send_chat_history_messages

將語意核心的聊天歷史訊息傳送到 MCP 平台。

此方法接受一連串語意核心 ChatMessageContent 物件,將其轉換為 ChatHistoryMessage 格式,並傳送至 MCP 平台以提供即時威脅防護。

__init__

初始化語意核心的 MCP 工具註冊服務。

__init__(logger: Logger | None = None)

參數

名稱 Description
logger

用於日誌作業的記錄器實例。

預設值: None

__new__

__new__(**kwargs)

add_tool_servers_to_agent

將 A365 MCP 工具伺服器加入指定核心。

async add_tool_servers_to_agent(kernel: Kernel, auth: Authorization, auth_handler_name: str, context: TurnContext, auth_token: str | None = None) -> None

參數

名稱 Description
kernel
必要

工具將被加入的語意核心實例。

auth
必要

用於代幣交換的授權處理程序。

auth_handler_name
必要
str

授權處理者的名字。

context
必要

請說明當前行動的背景。

auth_token
str | None

使用認證令牌來存取 MCP 伺服器。

預設值: None

傳回

類型 Description

例外狀況

類型 Description

若核為 None 或 必要參數無效。

如果連接或設定 MCP 伺服器時出現錯誤,

cleanup_connections

清理所有連接的 MCP 插件。

async cleanup_connections() -> None

傳回

類型 Description

send_chat_history

將語意核心的聊天紀錄傳送到 MCP 平台。

此方法從語意核心 ChatHistory 物件中擷取訊息,轉換為 ChatHistoryMessage 格式,並傳送至 MCP 平台以進行即時威脅防護。

async send_chat_history(turn_context: TurnContext, chat_history: ChatHistory, limit: int | None = None, options: ToolOptions | None = None) -> OperationResult

參數

名稱 Description
turn_context
必要

來自 Agents SDK 的 TurnContext,包含對話資訊。

chat_history
必要

包含訊息的語意核心 ChatHistory 物件。

limit
int | None

可選地設定最多可發送的訊息數量。 若指定,則發送最近的 N 則訊息。 若無,則發送所有訊息。

預設值: None
options

請求的可選設定。

預設值: None

傳回

類型 Description

OperationResult 表示成功或失敗。

例外狀況

類型 Description

如果turn_context或chat_history則是無。

範例


>>> from semantic_kernel.contents import ChatHistory
>>> from microsoft_agents_a365.tooling.extensions.semantickernel import (
...     McpToolRegistrationService
... )
>>>
>>> service = McpToolRegistrationService()
>>> chat_history = ChatHistory()
>>> chat_history.add_user_message("Hello!")
>>> chat_history.add_assistant_message("Hi there!")
>>>
>>> result = await service.send_chat_history(
...     turn_context, chat_history, limit=50
... )
>>> if result.succeeded:
...     print("Chat history sent successfully")

send_chat_history_messages

將語意核心的聊天歷史訊息傳送到 MCP 平台。

此方法接受一連串語意核心 ChatMessageContent 物件,將其轉換為 ChatHistoryMessage 格式,並傳送至 MCP 平台以提供即時威脅防護。

async send_chat_history_messages(turn_context: TurnContext, messages: Sequence[ChatMessageContent], options: ToolOptions | None = None) -> OperationResult

參數

名稱 Description
turn_context
必要

來自 Agents SDK 的 TurnContext,包含對話資訊。

messages
必要

發送語意核心 ChatMessageContent 物件的順序。

options

請求的可選設定。

預設值: None

傳回

類型 Description

OperationResult 表示成功或失敗。

例外狀況

類型 Description

如果turn_context或訊息是「無」。

範例


>>> from semantic_kernel.contents import ChatMessageContent, AuthorRole
>>> from microsoft_agents_a365.tooling.extensions.semantickernel import (
...     McpToolRegistrationService
... )
>>>
>>> service = McpToolRegistrationService()
>>> messages = [
...     ChatMessageContent(role=AuthorRole.USER, content="Hello!"),
...     ChatMessageContent(role=AuthorRole.ASSISTANT, content="Hi there!"),
... ]
>>>
>>> result = await service.send_chat_history_messages(
...     turn_context, messages
... )
>>> if result.succeeded:
...     print("Chat history sent successfully")