# Directory Structure

```
├── .gitignore
├── img
│   └── inspector.png
├── LICENSE
├── package-lock.json
├── package.json
├── README.md
├── src
│   ├── cli.ts
│   └── index.ts
└── tsconfig.json
```

# Files

--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------

```
1 | node_modules/
2 | build/
3 | *.log
4 | .env*
5 | .DS_Store
6 | 
```

--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------

```markdown
  1 | # amazon-ads-mcp-server
  2 | 
  3 | Connect to your Amazon Advertising Data by integrating your account with [MarketplaceAdPros](https://marketplaceadpros.com).
  4 | 
  5 | Provides access to:
  6 | 
  7 | - Advertising Resources in Sponsored Products, Sponsored Brands and Sponsored Display, like Campaigns, Ad Groups, Keywords, Product Ads, Targeting
  8 | - Reports and ability to query them with plain english.
  9 | - Marketplace Ad Pros Recommendations, Experiments and more with purchased subscription plan
 10 | 
 11 | Also available as a Streamable HTTP MCP Server by connecting to `https://app.marketplaceadpros.com/mcp`
 12 | 
 13 | ## Installation
 14 | 
 15 | To add the amazon-ads-mcp-server to your MCP client of choice, add the following to the server config:
 16 | 
 17 | On MacOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
 18 | 
 19 | On Windows: `%APPDATA%/Claude/claude_desktop_config.json`
 20 | 
 21 | ### Env Vars
 22 | 
 23 | - `BEARER_TOKEN`: The Bearer token you got from MarketplaceAdPros.com
 24 | 
 25 | 
 26 | ### Configuration
 27 | 
 28 | You can use it via `npx` in your Claude Desktop configuration like this:
 29 | 
 30 | ```json
 31 | {
 32 |   "mcpServers": {
 33 |     "marketplaceadpros": {
 34 |       "command": "npx",
 35 |       "args": [
 36 |         "@marketplaceadpros/amazon-ads-mcp-server"
 37 |       ],
 38 |       "env": {
 39 |         "BEARER_TOKEN": "abcdefghijklmnop"
 40 |       }
 41 |     }
 42 |   }
 43 | }
 44 | ```
 45 | 
 46 | 
 47 | Or, if you clone the repo, you can build and use in your Claude Desktop configuration like this:
 48 | 
 49 | 
 50 | ```json
 51 | 
 52 | {
 53 |   "mcpServers": {
 54 |     "marketplaceadpros": {
 55 |       "command": "node",
 56 |       "args": [
 57 |         "/path/to/amazon-ads-mcp-server/build/index.js"
 58 |       ],
 59 |       "env": {
 60 |         "BEARER_TOKEN": "abcdefghijklmnop"
 61 |       }
 62 |     }
 63 |   }
 64 | }
 65 | ```
 66 | 
 67 | 
 68 | Or, if your client supports the Streamable HTTP MCP Servers, you can just point to the MCP endpoint at `https://app.marketplaceadpros.com/mcp`. 
 69 | 
 70 | 
 71 | ```json
 72 | 
 73 | {
 74 |   "mcpServers": {
 75 |     "marketplaceadpros": {
 76 |       "type": "streamable-http",
 77 |       "url": "https://app.marketplaceadpros.com/mcp"
 78 |     }
 79 |   }
 80 | }
 81 | ```
 82 | 
 83 | 
 84 | Or, configure in [LibreChat](https://www.librechat.ai/) like:
 85 | ```yaml
 86 |   MAP:
 87 |     type: streamable-http
 88 |     url: https://app.marketplaceadpros.com/mcp
 89 |     headers:
 90 |       Authorization: "Bearer abcdefghijklmnop"
 91 | ````
 92 | 
 93 | 
 94 | ## Development
 95 | 
 96 | Install dependencies:
 97 | ```bash
 98 | npm install
 99 | ```
100 | 
101 | Build the server:
102 | ```bash
103 | npm run build
104 | ```
105 | 
106 | For development with auto-rebuild:
107 | ```bash
108 | npm run watch
109 | ```
110 | 
111 | ### Debugging
112 | 
113 | Since MCP servers communicate over stdio, debugging can be challenging. We recommend using the [MCP Inspector](https://github.com/modelcontextprotocol/inspector), which is available as a package script:
114 | 
115 | ```bash
116 | npm run inspector
117 | ```
118 | 
119 | ![amazon-ads-mcp-server live in inspector](img/inspector.png)
120 | 
121 | The Inspector will provide a URL to access debugging tools in your browser.
122 | 
123 | ### Acknowledgements
124 | 
125 | - Obviously the modelcontextprotocol and Anthropic teams for the MCP Specification. [https://modelcontextprotocol.io/introduction](https://modelcontextprotocol.io/introduction)
126 | - [MarketplaceAdPros](https://marketplaceadpros.com?ref=github-amazon-ads-mcp-server) for enabling and sponsoring this project.
127 | 
```

--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------

```json
 1 | {
 2 |   "compilerOptions": {
 3 |     "target": "ES2022",
 4 |     "module": "Node16",
 5 |     "moduleResolution": "Node16",
 6 |     "outDir": "./build",
 7 |     "rootDir": "./src",
 8 |     "strict": true,
 9 |     "esModuleInterop": true,
10 |     "skipLibCheck": true,
11 |     "forceConsistentCasingInFileNames": true
12 |   },
13 |   "include": ["src/**/*"],
14 |   "exclude": ["node_modules"]
15 | }
16 | 
```

--------------------------------------------------------------------------------
/src/cli.ts:
--------------------------------------------------------------------------------

```typescript
 1 | #!/usr/bin/env node
 2 | 
 3 | import { spawn } from 'child_process';
 4 | import { fileURLToPath } from 'url';
 5 | import { dirname, join } from 'path';
 6 | 
 7 | // Get the directory of the current module
 8 | const __filename = fileURLToPath(import.meta.url);
 9 | const __dirname = dirname(__filename);
10 | 
11 | // Path to the main index.js file
12 | const serverPath = join(__dirname, 'index.js');
13 | 
14 | // Spawn the server process
15 | const server = spawn('node', [serverPath], {
16 |   stdio: 'inherit',
17 |   env: process.env
18 | });
19 | 
20 | // Handle process exit
21 | server.on('exit', (code) => {
22 |   process.exit(code || 0);
23 | });
24 | 
25 | // Handle signals to properly terminate the child process
26 | process.on('SIGINT', () => {
27 |   server.kill('SIGINT');
28 | });
29 | 
30 | process.on('SIGTERM', () => {
31 |   server.kill('SIGTERM');
32 | });
33 | 
34 | 
```

--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------

```json
 1 | {
 2 |   "name": "@marketplaceadpros/amazon-ads-mcp-server",
 3 |   "version": "0.1.1",
 4 |   "description": "A Model Context Protocol Server that runs as stdio, pointing to MarketplaceAdPros for Amazon Ads integration and MCP integration",
 5 |   "type": "module",
 6 |   "bin": {
 7 |     "amazon-ads-mcp-server": "./build/cli.js"
 8 |   },
 9 |   "files": [
10 |     "build"
11 |   ],
12 |   "scripts": {
13 |     "build": "tsc && node -e \"require('fs').chmodSync('build/index.js', '755'); require('fs').chmodSync('build/cli.js', '755')\"",
14 |     "prepare": "npm run build",
15 |     "watch": "tsc --watch",
16 |     "inspector": "npx @modelcontextprotocol/inspector build/index.js",
17 |     "clean": "rm -rf build",
18 |     "npm-publish": "npm run clean && npm run build && npm publish --access=public",
19 |     "start": "node build/cli.js"
20 |   },
21 |   "dependencies": {
22 |     "@modelcontextprotocol/sdk": "^1.11.0",
23 |     "dotenv": "^16.4.5"
24 |   },
25 |   "devDependencies": {
26 |     "@types/node": "^20.11.24",
27 |     "typescript": "^5.3.3"
28 |   },
29 |   "repository": {
30 |     "type": "git",
31 |     "url": "git+https://github.com/marketplaceadpros/amazon-ads-mcp-server.git"
32 |   },
33 |   "keywords": [
34 |     "claude",
35 |     "openai",
36 |     "mcp",
37 |     "model-context-protocol",
38 |     "ai",
39 |     "chat",
40 |     "llm"
41 |   ],
42 |   "author": "MarketplaceAdPros",
43 |   "license": "MIT",
44 |   "engines": {
45 |     "node": ">=18"
46 |   }
47 | }
48 | 
```

--------------------------------------------------------------------------------
/src/index.ts:
--------------------------------------------------------------------------------

```typescript
  1 | #!/usr/bin/env node
  2 | 
  3 | import dotenv from "dotenv";
  4 | import { Server } from "@modelcontextprotocol/sdk/server/index.js";
  5 | import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
  6 | import {
  7 |   CallToolRequestSchema,
  8 |   ListResourcesRequestSchema,
  9 |   ListToolsRequestSchema,
 10 |   ReadResourceRequestSchema,
 11 |   ListPromptsRequestSchema,
 12 |   GetPromptRequestSchema,
 13 | } from "@modelcontextprotocol/sdk/types.js";
 14 | 
 15 | // Deps to run as a client
 16 | import { Client } from "@modelcontextprotocol/sdk/client/index.js";
 17 | import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
 18 | 
 19 | 
 20 | dotenv.config();
 21 | 
 22 | const URI = process.env.URI || 'https://app.marketplaceadpros.com/mcp';
 23 | const BEARER_TOKEN = process.env.BEARER_TOKEN;
 24 | const NAME = process.env.MCP_NAME || 'amazon-ads-mcp-server';
 25 | 
 26 | if (!URI) {
 27 |   throw new Error("URI is required")
 28 | }
 29 | // console.log("starting...")
 30 | //
 31 | let transportOptions = {};
 32 | if (BEARER_TOKEN !== undefined) {
 33 |   transportOptions = {
 34 |     requestInit: {
 35 |       headers: {
 36 |         "Authorization": `Bearer ${BEARER_TOKEN}`
 37 |       }
 38 |     }
 39 |   }
 40 | }
 41 | 
 42 | const transport = new StreamableHTTPClientTransport(
 43 |     new URL(`${URI}`),
 44 |     transportOptions
 45 | );
 46 | // console.log("making client...")
 47 | 
 48 | const client = new Client(
 49 |   {
 50 |     name: NAME,
 51 |     version: "0.1.0"
 52 |   }
 53 | );
 54 | 
 55 | // console.log("connecting to transport...")
 56 | await client.connect(transport);
 57 | 
 58 | 
 59 | const server = new Server(
 60 |   {
 61 |     name: NAME,
 62 |     version: "0.1.0",
 63 |   },
 64 |   {
 65 |     capabilities: {
 66 |       tools: {},
 67 |     },
 68 |   }
 69 | );
 70 | 
 71 | /**
 72 |  * Handler for listing resources.
 73 |  */
 74 | // server.setRequestHandler(ListResourcesRequestSchema, async () => {
 75 | //   return await client.listResources();
 76 | // });
 77 | //
 78 | // /**
 79 | //  * Handler for reading the contents of a specific resource.
 80 | //  */
 81 | // server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
 82 | //   return await client.readResource({
 83 | //     uri: request.params.uri,
 84 | //   })
 85 | //
 86 | // });
 87 | 
 88 | /**
 89 |  * Handler that lists available tools.
 90 |  * Exposes a single "chat" tool that lets clients chat with another AI.
 91 |  */
 92 | server.setRequestHandler(ListToolsRequestSchema, async () => {
 93 |   return await client.listTools();
 94 | });
 95 | 
 96 | /**
 97 |  * Handler for the chat tool.
 98 |  * Connects to an OpenAI SDK compatible AI Integration.
 99 |  */
100 | server.setRequestHandler(CallToolRequestSchema, async (request) => {
101 |   return await client.callTool({
102 |     name: request.params.name,
103 |     arguments: request.params.arguments || {},
104 |   });
105 | });
106 | 
107 | /**
108 |  * Handler that lists available prompts.
109 |  */
110 | // server.setRequestHandler(ListPromptsRequestSchema, async () => {
111 | //   return await client.listPrompts();
112 | // });
113 | //
114 | // /**
115 | //  * Handler for the get prompt.
116 | //  */
117 | // server.setRequestHandler(GetPromptRequestSchema, async (request) => {
118 | //   return await client.getPrompt({
119 | //     name: request.params.name,
120 | //   });
121 | // });
122 | 
123 | /**
124 |  * Start the server using stdio transport.
125 |  * This allows the server to communicate via standard input/output streams.
126 |  */
127 | async function main() {
128 |   const transport = new StdioServerTransport();
129 |   await server.connect(transport);
130 | }
131 | 
132 | main().catch((error) => {
133 |   console.error("Server error:", error);
134 |   process.exit(1);
135 | });
136 | 
137 | 
138 | 
```

