McpToolRegistrationService 类

用于管理代理的 MCP 工具和服务器的服务

构造函数

McpToolRegistrationService()

参数

名称 说明
logger
默认值: None

方法

__init__

初始化 OPENAI 的 MCP 工具注册服务。

__new__
add_tool_servers_to_agent

通过创建新的代理实例将新的 MCP 服务器添加到代理。

注意:由于 OpenAI 代理 SDK 的限制,必须在创建代理期间设置 MCP 服务器。 如果找到新服务器,此方法将创建一个新的代理实例,其中包含所有 MCP 服务器(现有 + 新服务器)已正确初始化。

cleanup_all_servers

清理所有连接的 MCP 服务器

send_chat_history

从 OpenAI 会话中提取聊天历史记录并将其发送到 MCP 平台。

此方法使用 get_items() 从 OpenAI 会话对象中提取消息,并将其发送到 MCP 平台进行实时威胁防护。

send_chat_history_messages

将 OpenAI 聊天历史记录消息发送到 MCP 平台,以便进行威胁防护。

此方法接受 OpenAI TResponseInputItem 消息的列表,将其转换为 ChatHistoryMessage 格式,并将其发送到 MCP 平台。

注释

即使消息为空或在转换过程中筛选所有消息,

请求仍将发送到 MCP 平台。 这可确保用户

已正确注册来自 turn_context.activity.text 的消息

实时威胁防护。

__init__

初始化 OPENAI 的 MCP 工具注册服务。

__init__(logger: Logger | None = None)

参数

名称 说明
logger

日志记录作的记录器实例。

默认值: None

__new__

__new__(**kwargs)

add_tool_servers_to_agent

通过创建新的代理实例将新的 MCP 服务器添加到代理。

注意:由于 OpenAI 代理 SDK 的限制,必须在创建代理期间设置 MCP 服务器。 如果找到新服务器,此方法将创建一个新的代理实例,其中包含所有 MCP 服务器(现有 + 新服务器)已正确初始化。

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

参数

名称 说明
agent
必需
<xref:agents.agent.Agent>

要向其添加服务器的现有代理

auth
必需

令牌交换的授权处理程序。

auth_handler_name
必需
str

授权处理程序的名称。

context
必需

为当前作轮次上下文。

auth_token
str | None

用于访问 MCP 服务器的身份验证令牌。

默认值: None

返回

类型 说明
<xref:agents.agent.Agent>

包含所有 MCP 服务器的新代理实例,如果没有新服务器,则为原始代理实例

cleanup_all_servers

清理所有连接的 MCP 服务器

async cleanup_all_servers() -> None

返回

类型 说明

send_chat_history

从 OpenAI 会话中提取聊天历史记录并将其发送到 MCP 平台。

此方法使用 get_items() 从 OpenAI 会话对象中提取消息,并将其发送到 MCP 平台进行实时威胁防护。

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

参数

名称 说明
turn_context
必需

来自包含聊天信息的代理 SDK 的 TurnContext。 必须具有具有 conversation.id、activity.id 和 activity.text 的有效活动。

session
必需
<xref:agents.memory.session.Session>

用于从中提取消息的 OpenAI 会话实例。 必须支持返回 TResponseInputItem 列表的 get_items() 方法。

limit
int | None

要从会话检索的最大项数可选。 如果为 None,则检索所有项。

默认值: None
options

用于自定义的可选 ToolOptions。 如果未提供,请使用orchestrator_name=“OpenAI”的默认选项。

默认值: None

返回

类型 说明

指示成功或失败的 OperationResult。 成功时,返回 OperationResult.success()。 失败时,返回 OperationResult.failed(),并返回错误详细信息。

例外

类型 说明

如果turn_context为 None 或会话为 None。

示例


>>> from agents import Agent, Runner
>>> from microsoft_agents_a365.tooling.extensions.openai import (
...     McpToolRegistrationService
... )
>>>
>>> service = McpToolRegistrationService()
>>> agent = Agent(name="my-agent", model="gpt-4")
>>>
>>> # In your agent handler:
>>> async with Runner.run(agent, messages) as result:
...     session = result.session
...     op_result = await service.send_chat_history(
...         turn_context, session
...     )
...     if op_result.succeeded:
...         print("Chat history sent successfully")

send_chat_history_messages

将 OpenAI 聊天历史记录消息发送到 MCP 平台,以便进行威胁防护。

此方法接受 OpenAI TResponseInputItem 消息的列表,将其转换为 ChatHistoryMessage 格式,并将其发送到 MCP 平台。

注释

即使消息为空或在转换过程中筛选所有消息,

请求仍将发送到 MCP 平台。 这可确保用户

已正确注册来自 turn_context.activity.text 的消息

实时威胁防护。

async send_chat_history_messages(turn_context: TurnContext, messages: List[EasyInputMessageParam | Message | ResponseOutputMessageParam | ResponseFileSearchToolCallParam | ResponseComputerToolCallParam | ComputerCallOutput | ResponseFunctionWebSearchParam | ResponseFunctionToolCallParam | FunctionCallOutput | ToolSearchCall | ResponseToolSearchOutputItemParamParam | ResponseReasoningItemParam | ResponseCompactionItemParamParam | ImageGenerationCall | ResponseCodeInterpreterToolCallParam | LocalShellCall | LocalShellCallOutput | ShellCall | ShellCallOutput | ApplyPatchCall | ApplyPatchCallOutput | McpListTools | McpApprovalRequest | McpApprovalResponse | McpCall | ResponseCustomToolCallOutputParam | ResponseCustomToolCallParam | ItemReference], options: ToolOptions | None = None) -> OperationResult

参数

名称 说明
turn_context
必需

来自包含聊天信息的代理 SDK 的 TurnContext。 必须具有具有 conversation.id、activity.id 和 activity.text 的有效活动。

messages
必需
List[<xref:openai.types.responses.easy_input_message_param.EasyInputMessageParam> | <xref:openai.types.responses.response_input_item_param.Message> | <xref:openai.types.responses.response_output_message_param.ResponseOutputMessageParam> | <xref:openai.types.responses.response_file_search_tool_call_param.ResponseFileSearchToolCallParam> | <xref:openai.types.responses.response_computer_tool_call_param.ResponseComputerToolCallParam> | <xref:openai.types.responses.response_input_item_param.ComputerCallOutput> | <xref:openai.types.responses.response_function_web_search_param.ResponseFunctionWebSearchParam> | <xref:openai.types.responses.response_function_tool_call_param.ResponseFunctionToolCallParam> | <xref:openai.types.responses.response_input_item_param.FunctionCallOutput> | <xref:openai.types.responses.response_input_item_param.ToolSearchCall> | <xref:openai.types.responses.response_tool_search_output_item_param_param.ResponseToolSearchOutputItemParamParam> | <xref:openai.types.responses.response_reasoning_item_param.ResponseReasoningItemParam> | <xref:openai.types.responses.response_compaction_item_param_param.ResponseCompactionItemParamParam> | <xref:openai.types.responses.response_input_item_param.ImageGenerationCall> | <xref:openai.types.responses.response_code_interpreter_tool_call_param.ResponseCodeInterpreterToolCallParam> | <xref:openai.types.responses.response_input_item_param.LocalShellCall> | <xref:openai.types.responses.response_input_item_param.LocalShellCallOutput> | <xref:openai.types.responses.response_input_item_param.ShellCall> | <xref:openai.types.responses.response_input_item_param.ShellCallOutput> | <xref:openai.types.responses.response_input_item_param.ApplyPatchCall> | <xref:openai.types.responses.response_input_item_param.ApplyPatchCallOutput> | <xref:openai.types.responses.response_input_item_param.McpListTools> | <xref:openai.types.responses.response_input_item_param.McpApprovalRequest> | <xref:openai.types.responses.response_input_item_param.McpApprovalResponse> | <xref:openai.types.responses.response_input_item_param.McpCall> | <xref:openai.types.responses.response_custom_tool_call_output_param.ResponseCustomToolCallOutputParam> | <xref:openai.types.responses.response_custom_tool_call_param.ResponseCustomToolCallParam> | <xref:openai.types.responses.response_input_item_param.ItemReference>]

要发送的 OpenAI TResponseInputItem 消息的列表。 支持 UserMessage、AssistantMessage、SystemMessage 和其他 OpenAI 消息类型。 可以为空 - 请求仍将发送到从 turn_context.activity.text 注册用户消息。

options

用于自定义的可选 ToolOptions。 如果未提供,请使用orchestrator_name=“OpenAI”的默认选项。

默认值: None

返回

类型 说明

指示成功或失败的 OperationResult。 成功时,返回 OperationResult.success()。 失败时,返回 OperationResult.failed(),并返回错误详细信息。

例外

类型 说明

如果turn_context为 None 或消息为 None。

示例


>>> from microsoft_agents_a365.tooling.extensions.openai import (
...     McpToolRegistrationService
... )
>>>
>>> service = McpToolRegistrationService()
>>> messages = [
...     {"role": "user", "content": "Hello"},
...     {"role": "assistant", "content": "Hi there!"},
... ]
>>>
>>> result = await service.send_chat_history_messages(
...     turn_context, messages
... )
>>> if result.succeeded:
...     print("Chat history sent successfully")