企业MCP认证
使用Azure AI Search MCP服务器/客户端和摄入应用程序进行企业MCP身份验证。
概述
此项目为Azure AI搜索实现了一个模型上下文协议(MCP)服务器和客户端,具有OAuth身份验证和文档级权限过滤功能。它包括:
- MCP服务器:通过Azure(Entra ID)OAuth的FastMCP服务器
AzureProvider以及Azure AI搜索的OBO流程 - MCP客户端:使用FastMCP内置OAuth流的CLI客户端(基于浏览器)
- LangGraph重构代理:使用LangGraph进行智能文档搜索和检索的多代理客户端
- 摄入工具:创建具有权限筛选的Azure AI搜索索引,并上传示例文档
特性
- ✅ 通过FastMCP进行Azure AD(Entra ID)OAuth身份验证
AzureProvider - ✅ 代表Azure AI搜索的(OBO)令牌流
- ✅ 通过以下方式进行文档级访问控制
x_ms_query_source_authorization - ✅ 四种MCP工具:
search_documents,get_document,suggest,get_user_info - ✅ 使用USER_IDS进行权限过滤(
oid字段)和GROUP_IDS(group现场) - ✅ 搜索建议支持
- ✅ 用于智能查询处理的LangGraph ReAct代理
- ✅ 带有身份验证和MCP客户端管理的监控器图
安装
- 克隆存储库:
git clone https://github.com/denniszielke/enterprise-mcp-auth.git
cd enterprise-mcp-auth- 安装依赖项:
pip install -r requirements.txt
# Or install the package in development mode
pip install -e .- 设置Azure AD应用程序:
选项A:自动(推荐)
# Create MCP Server app
./scripts/create-server-app.sh
# Create MCP Client app
./scripts/create-client-app.sh看 scripts/README.md 详细说明。
选项B:手动
- 按照中的手动配置步骤进行操作 scripts/README.md
- 配置环境变量:
cp .env.example .env
# Edit .env with values from the app registration scripts配置
编辑 .env 包含您的Azure凭据的文件:
# Azure Search Configuration
AZURE_SEARCH_ENDPOINT=https://your-search-service.search.windows.net
AZURE_SEARCH_INDEX=documents
AZURE_SEARCH_ADMIN_KEY=your-admin-key
# Azure AD Configuration (server app registration)
AZURE_CLIENT_ID=your-server-app-client-id
AZURE_CLIENT_SECRET=your-server-app-client-secret
AZURE_TENANT_ID=your-tenant-id
# MCP Client Configuration
MCP_SERVER_URL=http://localhost:8000/mcp
# LangGraph CLI Agent (optional)
# MCP_BASE_URL=http://localhost:8000
# MCP_SCOPE=api://your-server-client-id/.default
# OPENAI_API_KEY=your-openai-api-key用法
1.创建索引和摄取文档
首先,创建具有权限过滤的Azure AI搜索索引,并上传示例文档:
python -m ai_search_ingestion.create_index_and_documents这将:
- 创建一个名为的索引
documents带有权限筛选字段 - 添加一个名为的建议者
sg - 上传8个示例文档
- 注入当前Azure CLI用户ID,
AI_SEARCH_QUERY_USER_ID,以及AI_SEARCH_QUERY_GROUP_ID进入每个文档oid/group领域
2.启动MCP服务器
使用AzureProvider OAuth身份验证启动MCP服务器:
python -m enterprise_mcp_auth.server.ai_search_mcp_server服务器将:
- 在端口8000上侦听(默认)
- 使用FastMCP
AzureProvider适用于Azure AD OAuth(通过浏览器处理授权代码流) - 使用OBO流获取Azure AI搜索令牌
- 应用文档级权限过滤
3.使用MCP客户端
使用CLI客户端与MCP服务器交互:
# List available tools
python -m enterprise_mcp_auth.client.ai_search_mcp_client list-tools
# Search documents
python -m enterprise_mcp_auth.client.ai_search_mcp_client search --query "security" --top 5
# Get a specific document
python -m enterprise_mcp_auth.client.ai_search_mcp_client get --id "doc1"
# Get suggestions
python -m enterprise_mcp_auth.client.ai_search_mcp_client suggest --query "sec" --top 5客户将:
- 使用FastMCP内置的OAuth流(
auth="oauth")这将打开您的浏览器进行Azure AD身份验证 - 自动处理令牌获取、缓存和刷新
- 通过MCP服务器执行请求的工具
4.使用LangGraph ReAct代理
对于使用自然语言的智能查询处理,请使用LangGraph ReAct代理:
# After installing with pip install -e .
mcp-agent "What documents mention security policies?"
# Or run directly with Python
python -m enterprise_mcp_auth.cli "What documents mention security policies?"
# Use verbose mode to see reasoning steps
mcp-agent "Find documents about authentication" --verbose
# Specify a different OpenAI model
mcp-agent "Search for compliance documents" --model gpt-4LangGraph代理将:
- 使用设备代码流(或客户端凭据,如果
AZURE_CLIENT_SECRET已设置) - 创建一个可以访问MCP工具的ReAct代理
- 处理您的自然语言查询
- 自动调用适当的工具(搜索、获取、建议)
- 根据检索到的文档返回全面的答案
交互示例:
$ mcp-agent "What are the security policies mentioned in the documents?"
🤖 LangGraph ReAct Agent for Azure AI Search
============================================================
🔐 Acquiring access token...
Tenant: your-tenant-id
Client: your-client-id
Scope: api://your-server-client-id/.default
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code XXXXXXXXX to authenticate.
✓ Token acquired for user: user@example.com
🔍 Query: What are the security policies mentioned in the documents?
🌐 MCP Server: http://localhost:8000
🤖 Model: gpt-4o-mini
============================================================
📝 Response:
Based on the search results, here are the security policies mentioned in the documents:
1. **Document Security Policy (doc1)**: This document outlines general security guidelines...
2. **Access Control Policy (doc3)**: Details the access control mechanisms...
These documents provide comprehensive security guidelines for the organization.
============================================================
✓ Query completed successfully建筑
权限筛选
系统通过以下方式实施文档级访问控制:
- 索引字段:
- oid:用户ID集合(user_IDs权限筛选器) - group:组ID集合(group_IDs权限筛选器)
- OAuth+OBO令牌流:
- 客户端通过基于浏览器的Azure AD OAuth进行身份验证(由FastMCP管理 AzureProvider) - MCP服务器接收上游Azure AD令牌 - 服务器使用OBO将其交换为Azure AI搜索令牌 - 令牌包含用户/组声明
- 查询时间筛选:
- 服务器通过以下方式传递OBO令牌 x_ms_query_source_authorization 参数 - Azure AI搜索根据令牌声明过滤结果 - 只有与用户匹配的文档 oid 或 group 被退回
MCP工具
- search_documents(查询:str,顶部:int=5)
- 在索引中进行全文搜索 - 返回前N个匹配的文档 - 应用权限筛选
- get_document(id:str)
- 按ID检索特定文档 - 应用权限筛选 - 如果找不到/无法访问,则返回文档或错误
- 建议(查询:str,top:int=5)
- 使用自动完成建议 sg 建议者 - 返回前N个建议 - 应用权限筛选
- get_user_info()
- 返回有关经过身份验证的Azure用户的信息 - 从访问令牌中提取声明(子、电子邮件、姓名等)
安全
- 通过FastMCP实现Azure AD OAuth 2.0
AzureProvider对于所有操作 - AzureProvider在内部处理令牌发放和验证
- 代表流确保用户上下文得到保留
- Azure AI Search实施的文档级访问控制
- 没有直接向客户公开管理员密钥
代理框架集成
该存储库现在包括对具有代理身份验证的代理框架的支持。这允许代理使用代理身份而不是用户凭据直接向Azure服务进行身份验证。
代理框架功能
- ✅ 使用Agent identity Python SDK创建和管理代理身份
- ✅ 使用Agent框架核心定义Agent蓝图
- ✅ 直接Azure AI搜索身份验证(绕过MCP服务器)
- ✅ Azure服务的基于令牌的身份验证
- ✅ 全面的错误处理和记录
代理框架组件
代理框架组织在 src/enterprise_mcp_auth/agent_framework/ 目录:
- 代理_牙科:管理代理身份创建和令牌获取
- agent_blueprint.py:定义具有功能的代理蓝图
- agent.py:与Azure AI Search集成的主代理实现
代理框架配置
将以下环境变量添加到您的 .env 文件:
# Agent Framework Configuration
AGENT_IDENTITY_CLIENT_ID=your-agent-identity-client-id
AGENT_IDENTITY_TENANT_ID=your-agent-identity-tenant-id
AGENT_IDENTITY_AUDIENCE=https://search.azure.com/.default
AGENT_BLUEPRINT_NAME=enterprise-blueprint
AGENT_NAME=enterprise-agent代理框架使用
1.设置代理身份
创建用于身份验证的代理标识:
python scripts/agent/setup_agent_identity.py此脚本:
- 初始化代理身份管理器
- 验证身份配置
- 使用指定名称创建代理标识
- 将其与蓝图联系起来
2.设置代理蓝图
定义具有以下功能的代理蓝图:
python scripts/agent/setup_agent_blueprint.py此脚本:
- 创建代理蓝图
- 定义功能(search_documents、retrieve_document、authenticate_to_aazure)
- 验证蓝图配置
3.运行代理
使用身份验证执行代理:
# Run with default search query
python scripts/agent/run_agent.py
# Run with custom search query
python scripts/agent/run_agent.py "security policies"代理人将:
- 使用代理身份进行身份验证
- 连接到Azure AI搜索
- 执行搜索查询
- 显示带有文档详细信息的结果
4.测试直接Azure AI搜索访问
在不使用MCP服务器的情况下测试Azure AI Search的直接身份验证:
# Test with default query
python scripts/agent/test_ai_search_direct.py
# Test with custom query
python scripts/agent/test_ai_search_direct.py "compliance"此脚本演示:
- 使用代理身份直接进行Azure AI搜索身份验证
- Azure AI搜索的令牌获取
- 文档搜索和检索
- 绕过MCP服务器以直接访问API
输出示例:
$ python scripts/agent/test_ai_search_direct.py "security"
============================================================
Direct Azure AI Search Test with Agent Identity
============================================================
Endpoint: https://your-search-service.search.windows.net
Index: documents
Search Query: security
Initializing Agent Identity Manager...
✓ Tenant ID: your-tenant-id
✓ Client ID: your-client-id
Acquiring access token for Azure AI Search...
✓ Token acquired (length: 1234)
Creating Azure Search client for index: documents...
✓ Search client created
Searching for: 'security'...
✓ Found 3 documents
Search Results:
------------------------------------------------------------
1. Document ID: doc1
Title: Document Security Policy
Content: This document outlines security guidelines...
Score: 2.4567
2. Document ID: doc3
Title: Access Control Policy
Content: Details about access control mechanisms...
Score: 1.8901
============================================================
Test completed successfully!
============================================================代理框架架构
Agent Identity
↓
Agent Blueprint
↓
Enterprise Agent
↓
Azure AI Search Client (Direct Auth)
↓
Search / Retrieve Documents关键部件:
- 代理部门经理:管理代理身份和令牌获取
- Agent蓝图管理器:定义代理功能和配置
- 企业代理:与Azure AI Search集成的主代理实现
- 令牌凭证:Azure SDK的简单凭据适配器
代理身份与用户身份
代理身份(新):
- 使用服务主体或托管身份
- 适用于自动化代理和服务
- 对Azure服务的直接身份验证
- 无需用户交互
- 非常适合后台任务和自动化
用户身份(现有):
- 通过FastMCP使用基于浏览器的OAuth流
AzureProvider - 需要用户身份验证
- 通过MCP服务器代表(OBO)流使用
- 基于用户声明的文档级访问控制
- 非常适合交互式应用程序
代理框架参考
发展
项目结构
enterprise_mcp_auth/
├── __init__.py
├── cli.py # LangGraph ReAct agent CLI
├── server/
│ ├── __init__.py
│ ├── __main__.py
│ └── ai_search_mcp_server.py
├── client/
│ ├── __init__.py
│ ├── __main__.py
│ ├── ai_search_mcp_client.py # MCP client (uses FastMCP built-in OAuth)
│ ├── auth.py # MSAL authentication helpers (used by LangGraph agent)
│ └── mcp_client.py # Authenticated MCP client wrapper (used by LangGraph agent)
├── agents/
│ ├── __init__.py
│ ├── state.py # Graph state definitions
│ ├── tools.py # LangChain tool wrappers
│ ├── react_agent.py # ReAct agent implementation
│ └── supervisor.py # Supervisor graph
└── agent_framework/
├── __init__.py
├── agent_identity.py # Agent identity management
├── agent_blueprint.py # Agent blueprint definition
└── agent.py # Main agent implementation
ai_search_ingestion/
├── __init__.py
├── __main__.py
└── create_index_and_documents.py # Index creation & document ingestion需求
- Python 3.8+
- fastmcp==2.14.5
- 平均海平面>=1.31.0
- azure搜索文档>=11.6.0
- 天蓝色核心>=1.32.0
- python dotenv>=1.0.0
- langgraph>=0.2.0
- langchain openai>=0.2.0
- langchain核心>=0.3.0
- 点击>=8.1.0
- 请求>=2.31.0
- 代理框架核心==1.0.0b260116
- 天蓝色标识==1.25.1
- 代理身份python sdk>=0.1.2
LangGraph架构
LangGraph ReAct代理实现遵循以下架构:
User Query
↓
Supervisor Graph
↓
1. Identity Validation
- Check identity context exists
- Validate access token
↓
2. ReAct Agent Node
- Create authenticated MCP client
- Initialize ReAct agent with MCP tools
- Process query using reasoning + action loop
↓
3. Tool Execution
- search_documents: Search index
- get_document: Retrieve by ID
- suggest: Get suggestions
- get_user_info: Get authenticated user info
↓
Response关键部件:
- 身份上下文:在图中携带用户身份和访问令牌
- 代理商状态:完整状态,包括消息、身份、查询和MCP URL
- MCPTools:MCP服务器工具的LangChain工具包装器
- 重新代理:使用LangGraph的
create_react_agentOpenAI LLM - 主管:协调身份验证和代理执行
许可证
麻省理工学院
