MCP人工智能代理项目
A. .NET 9 Azure函数 使用 Azure OpenAI 与:
- 依赖注入
- 通过配置
local.settings.json - 强类型模型
- 重试逻辑(Polly)
- 逐令牌流式传输AI响应
______________________________________________________________________
1.先决条件
- .NET 9 SDK
- (用于功能核心工具)
- 码头工人
- VS Code 随着 Azure 函数 扩展
______________________________________________________________________
2.安装Azure功能核心工具
全局安装Azure Functions CLI:
npm install -g azure-functions-core-tools@4 --unsafe-perm true______________________________________________________________________
3.本地运行
第一步——清洁和建造
转到 src 文件夹并运行:
dotnet clean ./Simaira-MCP-LangChain-Agent
dotnet build ./Simaira-MCP-LangChain-Agent步骤2–启动功能主机
运行详细日志记录:
func start --script-root ./Simaira-MCP-LangChain-Agent --verbose您的功能将在以下网址提供:
http://localhost:7072/api/______________________________________________________________________
4.VS代码调试
- 启动您的功能:
func start --script-root ./Simaira-MCP-LangChain-Agent --dotnet-isolated-debug- 在VS Code终端中, 查找工作进程ID (寻找
dotnet过程)。 - 更新
processId在.vscode/launch.json在...之下Attach to Isolated Worker配置:
{
"name": "Attach to Isolated Worker",
"type": "coreclr",
"request": "attach",
"processId": "12345"
}- 首选 运行和调试 → 选择 “依附于孤立的工人” → Press F5.
______________________________________________________________________
5.Docker命令
构建和运行:
docker build -f Simaira-MCP-LangChain-Agent/Dockerfile -t my-azure-functionv1.1 .
docker run -p 8080:80 --name myfunctiondebug my-azure-functionv1.1
然后访问:
http://localhost:8080/api/______________________________________________________________________
6.配置
选项A-作为对象
在中使用强类型节 local.settings.json:
"AzureOpenAI": {
"Endpoint": "https://your-endpoint.openai.azure.com/",
"Key": "your-key",
"DeploymentName": "gpt-35-turbo",
"ApiVersion": "2025-01-01-preview"
}选项B——作为价值
使用嵌套值(在使用Docker/env覆盖时很有用):
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"AzureOpenAI:Endpoint": "https://your-endpoint.openai.azure.com/",
"AzureOpenAI:Key": "your-key",
"AzureOpenAI:DeploymentName": "gpt-35-turbo",
"AzureOpenAI:ApiVersion": "2025-01-01-preview",
"Host:LocalHttpPort": "7072"
}注:\ 如果在这些配置中找不到值,应用程序将抛出错误,以防止在缺少设置的情况下运行。
______________________________________________________________________
7.API终点
此Azure Function应用程序中提供以下HTTP终结点:
1. MCPAgentAPI
- 网址:
/api/MCPAgentAPI - 方法:
GET,POST - 请求正文(POST):
{
"message": "Write a function for binary search in C#."
}- 答复:
{
"response": "public int BinarySearch(int[] arr, int target) { ... }"
}2.AskSimairaAPI
- 网址:
/api/AskSimairaAPI - 方法:
GET,POST - 请求正文(POST):
{
"message": "Explain how binary search works."
}- 答复:
{
"response": "Binary search is an algorithm that finds the position of a target value within a sorted array..."
}3.AskSimairaSDK
- 网址:
/api/AskSimairaSDK - 方法:
GET,POST - 请求正文(POST):
{
"message": "Write a function for quicksort in C#."
}- 答复:
{
"response": "public void QuickSort(int[] arr, int left, int right) { ... }"
}4.MCPAgentSDK
- 网址:
/api/MCPAgentSDK - 方法:
GET,POST - 请求正文(POST):
{
"message": "Write a function for merge sort in C#."
}- 答复:
{
"response": "public void MergeSort(int[] arr) { ... }"
}注:
- 所有端点都接受
message在请求正文中(forPOST). - 响应是使用Azure OpenAI GPT模型生成的AI内容。
