|
| 1 | +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +SPDX-License-Identifier: MIT-0 |
| 3 | + |
| 4 | +# MCP Integration |
| 5 | + |
| 6 | +The GenAI IDP solution provides MCP (Model Context Protocol) integration that enables external applications like Amazon Quick Suite to access IDP functionality through AWS Bedrock AgentCore Gateway. This allows third-party applications to query processed document data and perform analytics operations through natural language interfaces. |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +The MCP integration exposes IDP capabilities to external applications by: |
| 11 | + |
| 12 | +- **Analytics Gateway**: Provides natural language access to processed document analytics data |
| 13 | +- **Secure Authentication**: Uses AWS Cognito OAuth 2.0 for secure external application access |
| 14 | +- **MCP Protocol**: Implements Model Context Protocol for standardized tool integration |
| 15 | +- **Real-time Queries**: Enables external applications to query document processing results in real-time |
| 16 | +- **Extensible Architecture**: Designed to support additional IDP functionality in future releases |
| 17 | + |
| 18 | +## External Application Integration |
| 19 | + |
| 20 | +External applications can integrate with the IDP system through the AgentCore Gateway by: |
| 21 | + |
| 22 | +1. **Authentication**: Obtaining OAuth tokens from the IDP's Cognito User Pool |
| 23 | +2. **Gateway Connection**: Connecting to the AgentCore Gateway endpoint |
| 24 | +3. **Tool Discovery**: Discovering available analytics tools via MCP protocol |
| 25 | +4. **Query Execution**: Executing natural language queries against processed document data |
| 26 | + |
| 27 | +### Integration Flow |
| 28 | + |
| 29 | +``` |
| 30 | +External App → Cognito Auth → AgentCore Gateway → Analytics Lambda → IDP Data |
| 31 | +``` |
| 32 | + |
| 33 | +## Enabling and Disabling the Feature |
| 34 | + |
| 35 | +### During Stack Deployment |
| 36 | + |
| 37 | +The MCP integration is controlled by the `EnableMCP` parameter: |
| 38 | + |
| 39 | +**Enable MCP Integration:** |
| 40 | +```yaml |
| 41 | +EnableMCP: 'true' # Default value |
| 42 | +``` |
| 43 | +
|
| 44 | +**Disable MCP Integration:** |
| 45 | +```yaml |
| 46 | +EnableMCP: 'false' |
| 47 | +``` |
| 48 | +
|
| 49 | +When enabled, the stack automatically creates: |
| 50 | +- AgentCore Gateway Manager Lambda function |
| 51 | +- AgentCore Analytics Lambda function |
| 52 | +- External App Client in Cognito User Pool |
| 53 | +- Required IAM roles and policies |
| 54 | +- AgentCore Gateway resource |
| 55 | +
|
| 56 | +When disabled, these resources are not created, reducing deployment complexity and costs. |
| 57 | +
|
| 58 | +## Current Capabilities |
| 59 | +
|
| 60 | +### Analytics Agent |
| 61 | +
|
| 62 | +The current implementation provides an Analytics Agent that processes natural language queries about processed document data. The agent follows the AgentCore schema and provides a single tool interface: |
| 63 | +
|
| 64 | +#### search_genaiidp |
| 65 | +Provides information from the GenAI Intelligent Document Processing System and answers user questions using natural language queries. |
| 66 | +
|
| 67 | +**Input Schema:** |
| 68 | +- `query` (string, required): Natural language question about processed documents or analytics data |
| 69 | + |
| 70 | +**Capabilities:** |
| 71 | +- Query processed document statistics and metadata |
| 72 | +- Analyze document processing trends and patterns |
| 73 | +- Retrieve information about document types, processing status, and results |
| 74 | +- Generate analytics reports based on natural language requests |
| 75 | + |
| 76 | +**Example Queries:** |
| 77 | +- "How many documents were processed last month?" |
| 78 | +- "What are the most common document types?" |
| 79 | +- "Show me the processing success rate by document type" |
| 80 | +- "Which documents had the lowest confidence scores?" |
| 81 | +- "Generate a report of processing errors from the last week" |
| 82 | + |
| 83 | +## Implementation Details |
| 84 | + |
| 85 | +### Architecture Components |
| 86 | + |
| 87 | +1. **AgentCore Gateway Manager Lambda** |
| 88 | + - Creates and manages the AgentCore Gateway |
| 89 | + - Handles CloudFormation custom resource lifecycle |
| 90 | + - Configures JWT authorization using Cognito |
| 91 | + |
| 92 | +2. **AgentCore Analytics Lambda** |
| 93 | + - Implements MCP protocol following AgentCore schema |
| 94 | + - Processes natural language queries via search_genaiidp tool |
| 95 | + - Translates queries to appropriate backend operations |
| 96 | + - Returns structured responses in natural language |
| 97 | + |
| 98 | +3. **AgentCore Gateway** |
| 99 | + - AWS Bedrock AgentCore Gateway resource |
| 100 | + - Routes requests between external applications and analytics Lambda |
| 101 | + - Handles authentication and authorization |
| 102 | + |
| 103 | +### Authentication Flow |
| 104 | + |
| 105 | +1. **External Application** requests access token from Cognito |
| 106 | +2. **Cognito User Pool** validates credentials and returns JWT token |
| 107 | +3. **External Application** calls AgentCore Gateway with Bearer token |
| 108 | +4. **AgentCore Gateway** validates JWT token against Cognito |
| 109 | +5. **Analytics Lambda** processes the request and returns results |
| 110 | + |
| 111 | +### Data Access |
| 112 | + |
| 113 | +The Analytics Lambda has read-only access to: |
| 114 | +- **Analytics Database**: Glue catalog with processed document metadata |
| 115 | +- **Reporting Bucket**: S3 bucket containing analytics data and query results |
| 116 | +- **Configuration Tables**: DynamoDB tables with system configuration |
| 117 | +- **Tracking Tables**: DynamoDB tables with processing status |
| 118 | + |
| 119 | +## Cognito User Pool Utilization |
| 120 | + |
| 121 | +### User Pool Configuration |
| 122 | + |
| 123 | +The IDP solution creates a Cognito User Pool with: |
| 124 | +- **Domain**: Auto-generated unique domain (e.g., `stack-name-timestamp.auth.region.amazoncognito.com`) |
| 125 | +- **Password Policy**: Configurable security requirements |
| 126 | +- **User Management**: Admin-managed user creation |
| 127 | +- **OAuth Flows**: Authorization code flow for external applications |
| 128 | + |
| 129 | +### External App Client |
| 130 | + |
| 131 | +When MCP is enabled, an additional Cognito User Pool Client is created: |
| 132 | + |
| 133 | +**Client Configuration:** |
| 134 | +- **Client Name**: "External-App-Client" |
| 135 | +- **Client Secret**: Generated automatically |
| 136 | +- **Auth Flows**: USER_PASSWORD_AUTH, ADMIN_USER_PASSWORD_AUTH, REFRESH_TOKEN_AUTH |
| 137 | +- **OAuth Flows**: Authorization code flow |
| 138 | +- **OAuth Scopes**: openid, email, profile |
| 139 | +- **Callback URLs**: |
| 140 | + - CloudFront distribution URL |
| 141 | + - QuickSight OAuth callback |
| 142 | + - Cognito User Pool domain |
| 143 | + |
| 144 | +### Token Management |
| 145 | + |
| 146 | +External applications can obtain tokens using: |
| 147 | + |
| 148 | +**Client Credentials Flow:** |
| 149 | +```bash |
| 150 | +curl -X POST <MCPTokenURL> \ |
| 151 | + -H "Content-Type: application/x-www-form-urlencoded" \ |
| 152 | + -d "grant_type=client_credentials&client_id=<MCPClientId>&client_secret=<MCPClientSecret>" |
| 153 | +``` |
| 154 | + |
| 155 | +**User Authentication Flow:** |
| 156 | +```bash |
| 157 | +# Step 1: Get authorization code |
| 158 | +<MCPAuthorizationURL>? |
| 159 | + response_type=code& |
| 160 | + client_id=<MCPClientId>& |
| 161 | + redirect_uri=CALLBACK_URL& |
| 162 | + scope=openid+email+profile |
| 163 | +
|
| 164 | +# Step 2: Exchange code for tokens |
| 165 | +curl -X POST <MCPTokenURL> \ |
| 166 | + -H "Content-Type: application/x-www-form-urlencoded" \ |
| 167 | + -d "grant_type=authorization_code&client_id=<MCPClientId>&client_secret=<MCPClientSecret>&code=AUTH_CODE&redirect_uri=CALLBACK_URL" |
| 168 | +``` |
| 169 | + |
| 170 | +## Output Parameters |
| 171 | + |
| 172 | +When MCP integration is enabled, the CloudFormation stack provides the following outputs required for external application integration: |
| 173 | + |
| 174 | +### MCP Server Endpoint |
| 175 | + |
| 176 | +- **`MCPServerEndpoint`**: The HTTPS endpoint for the MCP Server |
| 177 | + - The AgentCore Gateway URL for MCP protocol communication |
| 178 | + - Required for external applications to connect to the gateway via MCP protocol |
| 179 | + |
| 180 | +### Authentication Outputs |
| 181 | + |
| 182 | +- **`MCPClientId`**: Cognito User Pool Client ID for MCP authentication |
| 183 | + - Required for OAuth authentication flows |
| 184 | + - Used in token requests and API calls |
| 185 | + |
| 186 | +- **`MCPClientSecret`**: Cognito User Pool Client Secret for MCP authentication |
| 187 | + - Required for client authentication in OAuth flows |
| 188 | + - Should be securely stored and rotated regularly |
| 189 | + |
| 190 | +- **`MCPUserPool`**: Cognito User Pool ID for MCP authentication |
| 191 | + - Required for token validation and user management |
| 192 | + - Used by external applications for authentication setup |
| 193 | + |
| 194 | +- **`MCPTokenURL`**: OAuth token endpoint URL |
| 195 | + - Format: `https://domain-name.auth.region.amazoncognito.com/oauth2/token` |
| 196 | + - Used for obtaining access tokens via OAuth flows |
| 197 | + |
| 198 | +- **`MCPAuthorizationURL`**: OAuth authorization endpoint URL |
| 199 | + - Format: `https://domain-name.auth.region.amazoncognito.com/oauth2/authorize` |
| 200 | + - Used for initiating OAuth authorization code flows |
| 201 | + |
| 202 | +## Usage Examples |
| 203 | + |
| 204 | +### External Application Setup |
| 205 | + |
| 206 | +```python |
| 207 | +import requests |
| 208 | +import json |
| 209 | +
|
| 210 | +# Configuration from CloudFormation outputs |
| 211 | +GATEWAY_URL = "<MCPServerEndpoint>" # From stack outputs |
| 212 | +CLIENT_ID = "<MCPClientId>" # From stack outputs |
| 213 | +CLIENT_SECRET = "<MCPClientSecret>" # From stack outputs |
| 214 | +TOKEN_URL = "<MCPTokenURL>" # From stack outputs |
| 215 | +
|
| 216 | +# Get access token |
| 217 | +token_response = requests.post( |
| 218 | + TOKEN_URL, |
| 219 | + headers={"Content-Type": "application/x-www-form-urlencoded"}, |
| 220 | + data={ |
| 221 | + "grant_type": "client_credentials", |
| 222 | + "client_id": CLIENT_ID, |
| 223 | + "client_secret": CLIENT_SECRET |
| 224 | + } |
| 225 | +) |
| 226 | +access_token = token_response.json()["access_token"] |
| 227 | +
|
| 228 | +# Query analytics data using natural language |
| 229 | +query_request = { |
| 230 | + "method": "tools/call", |
| 231 | + "params": { |
| 232 | + "name": "search_genaiidp", |
| 233 | + "arguments": { |
| 234 | + "query": "How many documents were processed this week?" |
| 235 | + } |
| 236 | + } |
| 237 | +} |
| 238 | +
|
| 239 | +response = requests.post( |
| 240 | + GATEWAY_URL, |
| 241 | + headers={ |
| 242 | + "Authorization": f"Bearer {access_token}", |
| 243 | + "Content-Type": "application/json" |
| 244 | + }, |
| 245 | + json=query_request |
| 246 | +) |
| 247 | +
|
| 248 | +result = response.json() |
| 249 | +print(f"Query result: {result}") |
| 250 | +``` |
| 251 | + |
| 252 | +### Amazon Quick Suite Integration |
| 253 | + |
| 254 | +For Amazon Quick Suite integration, use the provided OAuth callback URLs and configure QuickSight to authenticate against the Cognito User Pool using the External App Client credentials. |
0 commit comments