使用Microsoft Agent Framework(Semantic Kernel ChatCompletionAgent)的Python代理,直接连接到Business Central。
pip install -r requirements.txt
BusinessCentralMCP.env:# Azure OpenAI
AZURE_OPENAI_API_KEY=your_key
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME=gpt-4o
# Business Central
BC_TENANT_ID=your_tenant_id
BC_CLIENT_ID=your_client_id
# BC_CLIENT_SECRET=your_secret # 可选 - 用于服务主体身份验证
BC_ENVIRONMENT_NAME=Production
BC_COMPANY_NAME=CRONUS International Ltd.
python bc_direct_agent.py
身份验证选项:
BC_CLIENT_SECRET以使用服务主体身份验证(无需用户交互)此实现遵循Microsoft Agent Framework模式:
用户问题
↓
ChatCompletionAgent (Semantic Kernel)
↓
BusinessCentralMCPPlugin (@kernel_function装饰器)
↓
MCP客户端会话(stdio_client)
↓
BcMCPProxy.exe (MCP服务器)
↓
Business Central API
semantic_kernel.agents的代理@kernel_function装饰器的插件Business Central MCP服务器(BcMCPProxy.exe)
BcMCPProxy.exe放置在此目录或在.env中更新路径Azure AD应用注册
ms-appx-web://Microsoft.AAD.BrokerPlugin/<clientID>Financials.ReadWrite.All(委托)user_impersonation(委托)Python 3.10+
Azure OpenAI或OpenAI API密钥
git clone <repository-url>
cd BusinessCentralMCPserver
pip install -r requirements.txt
BusinessCentralMCP.env并填写您的值:
编辑BusinessCentralMCP.env:
# Azure OpenAI
AZURE_OPENAI_API_KEY=your_key_here
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME=your_deployment
AZURE_OPENAI_API_VERSION=2_2024-08-01-preview
# Business Central MCP服务器
BC_MCP_SERVER_PATH=BcMCPProxy.exe
BC_TENANT_ID=your_tenant_id
BC_CLIENT_ID=your_client_id
BC_ENVIRONMENT_NAME=production
BC_COMPANY_NAME=CRONUS International Ltd.
BC_CONFIG_NAME=default
运行代理:
python bc_mcp_agent.py
代理将:
> 有哪些可用的工具?
[代理列出所有BC MCP工具]
> 显示客户信息
[代理调用适当的BC工具并显示结果]
> 列出最近的销售订单
[代理查询BC并格式化响应]
此实现使用与Semantic-Kernel-PlantRequestAgent参考中的完全相同模式:
内核设置
kernel = Kernel()
add_chat_service(kernel) # Azure OpenAI或OpenAI
插件创建
class BusinessCentralMCPPlugin:
@kernel_function(description="...", name="...")
async def call_bc_tool(self, tool_name: str, arguments: str) -> str:
# 调用MCP服务器
插件注册
kernel.add_plugin(bc_plugin, plugin_name="business_central")
代理创建
agent = ChatCompletionAgent(
name="BusinessCentralAgent",
instructions=system_prompt,
kernel=kernel,
function_choice_behavior=FunctionChoiceBehavior.Auto()
)
交互循环
async for response_item in agent.invoke(user_input):
print(response_item.content, end="")
代理通过stdio(标准输入/输出)连接到BcMCPProxy.exe:
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
# 使用会话调用工具
PlantRequestAgent直接对Business Central进行HTTP API调用:
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=headers)
而这个BC MCP代理通过MCP服务器连接:
result = await session.call_tool(tool_name, arguments=args_dict)
MCP方法的优点:
❌ 错误:BC MCP服务器未在以下位置找到:BcMCPProxy.exe
解决方案:在.env中更新BC_MCP_SERVER_PATH为BcMCPProxy.exe的完整路径
❌ 连接到BC MCP服务器时出错:...
解决方案:
解决方案:
MIT许可证