mcp-csp-investigation
Perform exhaustive Content Security Policy audits for MCP Apps running in sandboxed iframes where all network requests fail SILENTLY without proper CSP declarations.
Overview
MCP Apps run in sandboxed iframes with no same-origin server. This means:
- ALL network requests fail silently without CSP -- no errors, no warnings, just silent failure
- Every external origin (CDN, API, font, image, WebSocket) must be declared in CSP
- CSP is configured in the
contents[] return from the registerAppResource read callback
- Missing even ONE origin causes that resource to silently not load
This skill provides a systematic methodology for discovering every network origin an app uses, tracing each to its source, and generating the correct CSP configuration.
Capabilities
Build Output Analysis
- Build the application and capture all output files (HTML, CSS, JS, assets)
- Search every file for network origin references
- Identify fetch/XHR targets, script sources, link hrefs, image sources, font URLs, iframe sources, WebSocket endpoints
Origin Tracing
- Trace each discovered origin to its source in the codebase
- Classify as: hardcoded constant, environment variable, or conditional logic
- Document whether origin is universal, dev-only, or prod-only
- Check third-party libraries for hidden network requests (analytics, telemetry, CDN fallbacks)
CSP Domain Categorization
- resourceDomains: Scripts, stylesheets, images, fonts (maps to
script-src, style-src, img-src, font-src)
- connectDomains: fetch/XHR targets, WebSocket endpoints (maps to
connect-src)
- frameDomains: Nested iframes (maps to
frame-src)
CSP Configuration Generation
- Generate the CSP object for
registerAppResource read callback
- Handle environment-specific origins with proper conditional logic
- Verify conditional origins have matching runtime URL and CSP entry
Usage
Step 1: Build the Application
# Build to produce final output files
npm run build
# Identify all output files
find dist/ -type f \( -name "*.html" -o -name "*.js" -o -name "*.css" \)
Step 2: Search for ALL Network Origins
# Search for URL patterns in build output
grep -rEoh 'https?://[a-zA-Z0-9._-]+[a-zA-Z0-9._/-]*' dist/ | sort -u
# Search for protocol-relative URLs
grep -rEoh '//[a-zA-Z0-9._-]+\.[a-zA-Z]{2,}' dist/ | sort -u
# Search for fetch/XHR patterns in source
grep -rn 'fetch\|XMLHttpRequest\|axios\|\.get\(\|\.post\(' src/
# Search for WebSocket connections
grep -rn 'new WebSocket\|wss://\|ws://' src/
# Search for dynamic imports and lazy loading
grep -rn 'import(\|require(\|loadScript' src/
Step 3: Trace Each Origin to Source
For each discovered origin, determine:
| Origin |
Source Type |
Environment |
Category |
https://cdn.example.com |
Hardcoded constant |
Universal |
resourceDomains |
https://api.example.com |
Environment variable (API_URL) |
Conditional |
connectDomains |
https://fonts.googleapis.com |
Third-party library |
Universal |
resourceDomains |
wss://realtime.example.com |
Conditional (feature flag) |
Prod-only |
connectDomains |
Step 4: Check Third-Party Libraries
# Check node_modules for hidden network requests
grep -rn 'fetch\|XMLHttpRequest\|beacon\|sendBeacon' node_modules/<lib>/dist/ 2>/dev/null
# Common hidden request sources:
# - Analytics (Google Analytics, Segment, Mixpanel)
# - Error tracking (Sentry, Bugsnag, Datadog)
# - Font loading (Google Fonts, Adobe Fonts)
# - CDN fallbacks (jQuery CDN, unpkg)
# - Map tiles (Mapbox, Google Maps, Leaflet)
Step 5: Generate CSP Configuration
The CSP object goes in the contents[] return from registerAppResource:
import { registerAppResource, RESOURCE_MIME_TYPE } from '@modelcontextprotocol/ext-apps';
registerAppResource(server, {
uri: 'app:///my-app',
name: 'My App',
mimeType: RESOURCE_MIME_TYPE,
async read() {
return {
contents: [{
uri: 'app:///my-app',
mimeType: RESOURCE_MIME_TYPE,
text: bundledHtml,
// CSP configuration goes HERE, in contents[]
resourceDomains: [
'https://cdn.example.com',
'https://fonts.googleapis.com',
'https://fonts.gstatic.com',
],
connectDomains: [
'https://api.example.com',
...(process.env.NODE_ENV === 'development'
? ['http://localhost:3000']
: []),
],
frameDomains: [],
}],
};
},
});
Step 6: Verify Conditional Origins
For every origin that depends on configuration or environment:
// WRONG: Runtime URL uses config but CSP doesn't include it
const apiUrl = process.env.API_URL || 'https://api.example.com';
fetch(apiUrl); // Works for default, fails for custom API_URL
// RIGHT: Same config controls both runtime URL and CSP entry
const apiUrl = process.env.API_URL || 'https://api.example.com';
// In registerAppResource:
connectDomains: [apiUrl], // CSP matches runtime URL
Common Pitfalls
- Silent failures: Missing CSP origin = resource silently doesn't load. No console error, no network error.
- CSP in wrong location: CSP goes in
contents[] from registerAppResource read callback, NOT in _meta on the tool.
- Forgetting localhost: Even
http://localhost:3000 needs CSP in dev mode.
- Third-party surprises: Libraries like Google Maps, Sentry, or analytics SDKs make network requests you may not be aware of.
- Conditional origins not in CSP: If
API_URL can be customized, the CSP must include whatever value it resolves to.
- Snake_case CSP properties: Use camelCase (
resourceDomains, connectDomains, frameDomains), NOT snake_case.
Verification Checklist
Task Definition
const mcpCspInvestigationTask = defineTask({
name: 'mcp-csp-investigation',
description: 'Perform CSP audit for MCP App in sandboxed iframe',
inputs: {
projectDir: { type: 'string', required: true },
buildCommand: { type: 'string', default: 'npm run build' },
outputDir: { type: 'string', default: 'dist' }
},
outputs: {
resourceDomains: { type: 'array' },
connectDomains: { type: 'array' },
frameDomains: { type: 'array' },
conditionalOrigins: { type: 'array' },
artifacts: { type: 'array' }
},
async run(inputs, taskCtx) {
return {
kind: 'skill',
title: `CSP investigation for ${inputs.projectDir}`,
skill: {
name: 'mcp-csp-investigation',
context: {
projectDir: inputs.projectDir,
buildCommand: inputs.buildCommand,
outputDir: inputs.outputDir,
instructions: [
'Build the application',
'Search all output for network origins',
'Trace each origin to source',
'Check third-party libraries for hidden requests',
'Categorize domains (resource, connect, frame)',
'Generate CSP configuration for registerAppResource',
'Verify conditional origins have matching CSP entries'
]
}
},
io: {
inputJsonPath: `tasks/${taskCtx.effectId}/input.json`,
outputJsonPath: `tasks/${taskCtx.effectId}/result.json`
}
};
}
});
Applicable Processes
- convert-web-app-to-mcp.js
- migrate-openai-app-to-mcp.js
External Dependencies
- Build toolchain for the target application
- grep/ripgrep for origin discovery
- Access to third-party library source code or documentation
References
Related Skills
- mcp-app-verification
- mcp-tool-resource-pattern
- single-file-bundling
Related Agents
- csp-security-auditor
- mcp-app-architect
1---
2name: mcp-csp-investigation
3description: Comprehensive Content Security Policy audit for MCP Apps in sandboxed iframes. Discovers all network origins, traces them to source, and generates CSP configuration for registerAppResource.
4allowed-tools: Read, Bash, Glob, Grep
5graph:
6 domains: [domain:software-engineering]
7 specializations: [specialization:ai-agents-conversational]
8 skillAreas: [skill-area:mcp-server-implementation, skill-area:web-security]
9 roles: [role:backend-engineer, role:fullstack-engineer]
10 workflows: [workflow:feature-development]
11 topics: [topic:api-design]
12
13metadata:
14 source: https://skillmd.com/skills/a5c-ai/mcp-csp-investigation
15---
16
17# mcp-csp-investigation
18
19Perform exhaustive Content Security Policy audits for MCP Apps running in sandboxed iframes where all network requests fail SILENTLY without proper CSP declarations.
20
21## Overview
22
23MCP Apps run in sandboxed iframes with no same-origin server. This means:
24- **ALL network requests fail silently** without CSP -- no errors, no warnings, just silent failure
25- Every external origin (CDN, API, font, image, WebSocket) must be declared in CSP
26- CSP is configured in the `contents[]` return from the `registerAppResource` read callback
27- Missing even ONE origin causes that resource to silently not load
28
29This skill provides a systematic methodology for discovering every network origin an app uses, tracing each to its source, and generating the correct CSP configuration.
30
31## Capabilities
32
33### Build Output Analysis
34- Build the application and capture all output files (HTML, CSS, JS, assets)
35- Search every file for network origin references
36- Identify fetch/XHR targets, script sources, link hrefs, image sources, font URLs, iframe sources, WebSocket endpoints
37
38### Origin Tracing
39- Trace each discovered origin to its source in the codebase
40- Classify as: hardcoded constant, environment variable, or conditional logic
41- Document whether origin is universal, dev-only, or prod-only
42- Check third-party libraries for hidden network requests (analytics, telemetry, CDN fallbacks)
43
44### CSP Domain Categorization
45- **resourceDomains**: Scripts, stylesheets, images, fonts (maps to `script-src`, `style-src`, `img-src`, `font-src`)
46- **connectDomains**: fetch/XHR targets, WebSocket endpoints (maps to `connect-src`)
47- **frameDomains**: Nested iframes (maps to `frame-src`)
48
49### CSP Configuration Generation
50- Generate the CSP object for `registerAppResource` read callback
51- Handle environment-specific origins with proper conditional logic
52- Verify conditional origins have matching runtime URL and CSP entry
53
54## Usage
55
56### Step 1: Build the Application
57
58```bash
59# Build to produce final output files
60npm run build
61
62# Identify all output files
63find dist/ -type f \( -name "*.html" -o -name "*.js" -o -name "*.css" \)
64```
65
66### Step 2: Search for ALL Network Origins
67
68```bash
69# Search for URL patterns in build output
70grep -rEoh 'https?://[a-zA-Z0-9._-]+[a-zA-Z0-9._/-]*' dist/ | sort -u
71
72# Search for protocol-relative URLs
73grep -rEoh '//[a-zA-Z0-9._-]+\.[a-zA-Z]{2,}' dist/ | sort -u
74
75# Search for fetch/XHR patterns in source
76grep -rn 'fetch\|XMLHttpRequest\|axios\|\.get\(\|\.post\(' src/
77
78# Search for WebSocket connections
79grep -rn 'new WebSocket\|wss://\|ws://' src/
80
81# Search for dynamic imports and lazy loading
82grep -rn 'import(\|require(\|loadScript' src/
83```
84
85### Step 3: Trace Each Origin to Source
86
87For each discovered origin, determine:
88
89| Origin | Source Type | Environment | Category |
90|--------|-----------|-------------|----------|
91| `https://cdn.example.com` | Hardcoded constant | Universal | resourceDomains |
92| `https://api.example.com` | Environment variable (`API_URL`) | Conditional | connectDomains |
93| `https://fonts.googleapis.com` | Third-party library | Universal | resourceDomains |
94| `wss://realtime.example.com` | Conditional (feature flag) | Prod-only | connectDomains |
95
96### Step 4: Check Third-Party Libraries
97
98```bash
99# Check node_modules for hidden network requests
100grep -rn 'fetch\|XMLHttpRequest\|beacon\|sendBeacon' node_modules/<lib>/dist/ 2>/dev/null
101
102# Common hidden request sources:
103# - Analytics (Google Analytics, Segment, Mixpanel)
104# - Error tracking (Sentry, Bugsnag, Datadog)
105# - Font loading (Google Fonts, Adobe Fonts)
106# - CDN fallbacks (jQuery CDN, unpkg)
107# - Map tiles (Mapbox, Google Maps, Leaflet)
108```
109
110### Step 5: Generate CSP Configuration
111
112The CSP object goes in the `contents[]` return from `registerAppResource`:
113
114```typescript
115import { registerAppResource, RESOURCE_MIME_TYPE } from '@modelcontextprotocol/ext-apps';
116
117registerAppResource(server, {
118 uri: 'app:///my-app',
119 name: 'My App',
120 mimeType: RESOURCE_MIME_TYPE,
121 async read() {
122 return {
123 contents: [{
124 uri: 'app:///my-app',
125 mimeType: RESOURCE_MIME_TYPE,
126 text: bundledHtml,
127 // CSP configuration goes HERE, in contents[]
128 resourceDomains: [
129 'https://cdn.example.com',
130 'https://fonts.googleapis.com',
131 'https://fonts.gstatic.com',
132 ],
133 connectDomains: [
134 'https://api.example.com',
135 ...(process.env.NODE_ENV === 'development'
136 ? ['http://localhost:3000']
137 : []),
138 ],
139 frameDomains: [],
140 }],
141 };
142 },
143});
144```
145
146### Step 6: Verify Conditional Origins
147
148For every origin that depends on configuration or environment:
149
150```typescript
151// WRONG: Runtime URL uses config but CSP doesn't include it
152const apiUrl = process.env.API_URL || 'https://api.example.com';
153fetch(apiUrl); // Works for default, fails for custom API_URL
154
155// RIGHT: Same config controls both runtime URL and CSP entry
156const apiUrl = process.env.API_URL || 'https://api.example.com';
157// In registerAppResource:
158connectDomains: [apiUrl], // CSP matches runtime URL
159```
160
161## Common Pitfalls
162
1631. **Silent failures**: Missing CSP origin = resource silently doesn't load. No console error, no network error.
1642. **CSP in wrong location**: CSP goes in `contents[]` from `registerAppResource` read callback, NOT in `_meta` on the tool.
1653. **Forgetting localhost**: Even `http://localhost:3000` needs CSP in dev mode.
1664. **Third-party surprises**: Libraries like Google Maps, Sentry, or analytics SDKs make network requests you may not be aware of.
1675. **Conditional origins not in CSP**: If `API_URL` can be customized, the CSP must include whatever value it resolves to.
1686. **Snake_case CSP properties**: Use camelCase (`resourceDomains`, `connectDomains`, `frameDomains`), NOT snake_case.
169
170## Verification Checklist
171
172- [ ] Application built and all output files identified
173- [ ] Every network origin in build output discovered
174- [ ] Each origin traced to source (constant / env var / conditional)
175- [ ] Third-party libraries checked for hidden requests
176- [ ] Origins categorized (resourceDomains / connectDomains / frameDomains)
177- [ ] Environment annotations applied (universal / dev-only / prod-only)
178- [ ] CSP configuration placed in `contents[]` of `registerAppResource` read callback
179- [ ] Conditional origins verified: config controls both runtime URL and CSP entry
180- [ ] camelCase property names used (not snake_case)
181
182## Task Definition
183
184```javascript
185const mcpCspInvestigationTask = defineTask({
186 name: 'mcp-csp-investigation',
187 description: 'Perform CSP audit for MCP App in sandboxed iframe',
188
189 inputs: {
190 projectDir: { type: 'string', required: true },
191 buildCommand: { type: 'string', default: 'npm run build' },
192 outputDir: { type: 'string', default: 'dist' }
193 },
194
195 outputs: {
196 resourceDomains: { type: 'array' },
197 connectDomains: { type: 'array' },
198 frameDomains: { type: 'array' },
199 conditionalOrigins: { type: 'array' },
200 artifacts: { type: 'array' }
201 },
202
203 async run(inputs, taskCtx) {
204 return {
205 kind: 'skill',
206 title: `CSP investigation for ${inputs.projectDir}`,
207 skill: {
208 name: 'mcp-csp-investigation',
209 context: {
210 projectDir: inputs.projectDir,
211 buildCommand: inputs.buildCommand,
212 outputDir: inputs.outputDir,
213 instructions: [
214 'Build the application',
215 'Search all output for network origins',
216 'Trace each origin to source',
217 'Check third-party libraries for hidden requests',
218 'Categorize domains (resource, connect, frame)',
219 'Generate CSP configuration for registerAppResource',
220 'Verify conditional origins have matching CSP entries'
221 ]
222 }
223 },
224 io: {
225 inputJsonPath: `tasks/${taskCtx.effectId}/input.json`,
226 outputJsonPath: `tasks/${taskCtx.effectId}/result.json`
227 }
228 };
229 }
230});
231```
232
233## Applicable Processes
234
235- convert-web-app-to-mcp.js
236- migrate-openai-app-to-mcp.js
237
238## External Dependencies
239
240- Build toolchain for the target application
241- grep/ripgrep for origin discovery
242- Access to third-party library source code or documentation
243
244## References
245
246- [MCP Apps CSP/CORS Guide](https://github.com/modelcontextprotocol/ext-apps/blob/main/docs/csp-cors.md)
247- [MCP Apps Testing Guide](https://github.com/modelcontextprotocol/ext-apps/blob/main/docs/testing-mcp-apps.md)
248- [MDN Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP)
249- [MCP Apps Patterns - CSP Section](https://github.com/modelcontextprotocol/ext-apps/blob/main/docs/patterns.md)
250
251## Related Skills
252
253- mcp-app-verification
254- mcp-tool-resource-pattern
255- single-file-bundling
256
257## Related Agents
258
259- csp-security-auditor
260- mcp-app-architect