商务中心MCP代理
Python代理使用 Microsoft代理框架 (语义内核ChatCompletionAgent)直接与Business Central连接。
✨ 特性
- 直接连接 -不需要exe文件!直接连接到Business Central MCP HTTP端点
- 交叉平台的 -适用于Windows、macOS和Linux
- 灵活的身份验证 -支持设备代码流(委托)和客户端凭据(应用程序)
- Microsoft代理框架 -使用语义内核ChatCompletionAgent模式
快速开始
- 安装依赖项:
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 # Optional - for service principal auth
BC_ENVIRONMENT_NAME=Production
BC_COMPANY_NAME=CRONUS International Ltd.- 跑:
python bc_direct_agent.py身份验证选项:
- 设备代码流(默认):交互式浏览器身份验证-代理提示您访问URL并输入代码
- 客户端凭证流:添加
BC_CLIENT_SECRET使用服务主体身份验证(无用户交互)
建筑
此实现遵循Microsoft代理框架模式:
User Question
↓
ChatCompletionAgent (Semantic Kernel)
↓
BusinessCentralMCPPlugin (@kernel_function decorators)
↓
MCP Client Session (stdio_client)
↓
BcMCPProxy.exe (MCP Server)
↓
Business Central API关键组件
- 语义内核 -核心框架
- ChatCompletionAgent -代理人来自
semantic_kernel.agents - BusinessCentralMCP插件 -插件与
@kernel_function装饰器 - 功能选择行为。自动() -启用自动函数调用
- MCP客户端会话 -通过stdio连接到BC MCP服务器
先决条件
- 业务中心MCP服务器(BcMCPProxy.exe)
- 下载/构建来源:https://github.com/microsoft/BCTech/tree/master/samples/BcMCPProxy - 地方 BcMCPProxy.exe 在此目录或更新路径中 .env
- Azure AD应用程序注册
- 在Azure门户中创建应用注册 - 添加重定向URL: ms-appx-web://Microsoft.AAD.BrokerPlugin/ - 添加API权限: - Financials.ReadWrite.All (委托) - user_impersonation (委托)
- Python 3.10+
- Azure OpenAI或OpenAI API密钥
安装
- 克隆此存储库:
git clone
cd BusinessCentralMCPserver- 安装依赖项:
pip install -r requirements.txt- 配置环境变量:
- 编辑 BusinessCentralMCP.env 并填写您的值: - Azure OpenAI凭据 - BC MCP服务器路径 - Azure租户/客户端ID - 不列颠哥伦比亚省环境和公司名称
配置
编辑 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=2024-08-01-preview
# Business Central MCP Server
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工具
- 启动交互式聊天会话
交互示例
> What tools are available?
[Agent lists all BC MCP tools]
> Show me customer information
[Agent calls appropriate BC tool and displays results]
> List recent sales orders
[Agent queries BC and formats the response]运作原理
Microsoft代理框架模式
此实现使用 精确模式 来自语义内核PlantRequestAgent参考:
- 内核设置
kernel = Kernel()
add_chat_service(kernel) # Azure OpenAI or OpenAI- 插件创建
class BusinessCentralMCPPlugin:
@kernel_function(description="...", name="...")
async def call_bc_tool(self, tool_name: str, arguments: str) -> str:
# Calls MCP server- 插件注册
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="")MCP连接
代理使用stdio(标准输入/输出)连接到BcMCPProxy.exe:
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
# Use session to call tools与PlantRequestAgent的区别
PlantRequestAgent制作 直接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方法的好处:
- 抽象身份验证(由MCP服务器处理)
- 简化了API访问
- 与其他MCP客户端兼容(Claude Desktop、VS Code)
- 集中式BC访问逻辑
故障排除
未找到MCP服务器
❌ Error: BC MCP Server not found at: BcMCPProxy.exe解决方案:更新 BC_MCP_SERVER_PATH 在 .env 包含BcMCPProxy.exe的完整路径
身份验证错误
❌ Error connecting to BC MCP Server: ...解决方案:
- 检查Azure AD应用程序注册
- 验证重定向URL格式
- 确保授予API权限
- 确认租户/客户ID正确
连接超时
解决方案:
- 验证BC环境名称是否正确
- 检查公司名称(区分大小写)
- 测试与BC的网络连接
参考文献
- BcMCP代理: https://github.com/microsoft/BCTech/tree/master/samples/BcMCPProxy
- MCP协议: https://modelcontextprotocol.io/
- 语义内核: https://learn.microsoft.com/en-us/semantic-kernel/
- Microsoft代理框架:语义内核ChatCompletionAgent的组合
许可证
MIT许可证
