#
tokens: 2329/50000 6/6 files
lines: off (toggle) GitHub
raw markdown copy
# Directory Structure

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

# Files

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

```
node_modules/
build/
*.log
.env*
.DS_Store

```

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

```markdown
# amazon-ads-mcp-server

Connect to your Amazon Advertising Data by integrating your account with [MarketplaceAdPros](https://marketplaceadpros.com).

Provides access to:

- Advertising Resources in Sponsored Products, Sponsored Brands and Sponsored Display, like Campaigns, Ad Groups, Keywords, Product Ads, Targeting
- Reports and ability to query them with plain english.
- Marketplace Ad Pros Recommendations, Experiments and more with purchased subscription plan

Also available as a Streamable HTTP MCP Server by connecting to `https://app.marketplaceadpros.com/mcp`

## Installation

To add the amazon-ads-mcp-server to your MCP client of choice, add the following to the server config:

On MacOS: `~/Library/Application Support/Claude/claude_desktop_config.json`

On Windows: `%APPDATA%/Claude/claude_desktop_config.json`

### Env Vars

- `BEARER_TOKEN`: The Bearer token you got from MarketplaceAdPros.com


### Configuration

You can use it via `npx` in your Claude Desktop configuration like this:

```json
{
  "mcpServers": {
    "marketplaceadpros": {
      "command": "npx",
      "args": [
        "@marketplaceadpros/amazon-ads-mcp-server"
      ],
      "env": {
        "BEARER_TOKEN": "abcdefghijklmnop"
      }
    }
  }
}
```


Or, if you clone the repo, you can build and use in your Claude Desktop configuration like this:


```json

{
  "mcpServers": {
    "marketplaceadpros": {
      "command": "node",
      "args": [
        "/path/to/amazon-ads-mcp-server/build/index.js"
      ],
      "env": {
        "BEARER_TOKEN": "abcdefghijklmnop"
      }
    }
  }
}
```


Or, if your client supports the Streamable HTTP MCP Servers, you can just point to the MCP endpoint at `https://app.marketplaceadpros.com/mcp`. 


```json

{
  "mcpServers": {
    "marketplaceadpros": {
      "type": "streamable-http",
      "url": "https://app.marketplaceadpros.com/mcp"
    }
  }
}
```


Or, configure in [LibreChat](https://www.librechat.ai/) like:
```yaml
  MAP:
    type: streamable-http
    url: https://app.marketplaceadpros.com/mcp
    headers:
      Authorization: "Bearer abcdefghijklmnop"
````


## Development

Install dependencies:
```bash
npm install
```

Build the server:
```bash
npm run build
```

For development with auto-rebuild:
```bash
npm run watch
```

### Debugging

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:

```bash
npm run inspector
```

![amazon-ads-mcp-server live in inspector](img/inspector.png)

The Inspector will provide a URL to access debugging tools in your browser.

### Acknowledgements

- Obviously the modelcontextprotocol and Anthropic teams for the MCP Specification. [https://modelcontextprotocol.io/introduction](https://modelcontextprotocol.io/introduction)
- [MarketplaceAdPros](https://marketplaceadpros.com?ref=github-amazon-ads-mcp-server) for enabling and sponsoring this project.

```

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

```json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "Node16",
    "moduleResolution": "Node16",
    "outDir": "./build",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

```

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

```typescript
#!/usr/bin/env node

import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

// Get the directory of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Path to the main index.js file
const serverPath = join(__dirname, 'index.js');

// Spawn the server process
const server = spawn('node', [serverPath], {
  stdio: 'inherit',
  env: process.env
});

// Handle process exit
server.on('exit', (code) => {
  process.exit(code || 0);
});

// Handle signals to properly terminate the child process
process.on('SIGINT', () => {
  server.kill('SIGINT');
});

process.on('SIGTERM', () => {
  server.kill('SIGTERM');
});


```

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

```json
{
  "name": "@marketplaceadpros/amazon-ads-mcp-server",
  "version": "0.1.1",
  "description": "A Model Context Protocol Server that runs as stdio, pointing to MarketplaceAdPros for Amazon Ads integration and MCP integration",
  "type": "module",
  "bin": {
    "amazon-ads-mcp-server": "./build/cli.js"
  },
  "files": [
    "build"
  ],
  "scripts": {
    "build": "tsc && node -e \"require('fs').chmodSync('build/index.js', '755'); require('fs').chmodSync('build/cli.js', '755')\"",
    "prepare": "npm run build",
    "watch": "tsc --watch",
    "inspector": "npx @modelcontextprotocol/inspector build/index.js",
    "clean": "rm -rf build",
    "npm-publish": "npm run clean && npm run build && npm publish --access=public",
    "start": "node build/cli.js"
  },
  "dependencies": {
    "@modelcontextprotocol/sdk": "^1.11.0",
    "dotenv": "^16.4.5"
  },
  "devDependencies": {
    "@types/node": "^20.11.24",
    "typescript": "^5.3.3"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/marketplaceadpros/amazon-ads-mcp-server.git"
  },
  "keywords": [
    "claude",
    "openai",
    "mcp",
    "model-context-protocol",
    "ai",
    "chat",
    "llm"
  ],
  "author": "MarketplaceAdPros",
  "license": "MIT",
  "engines": {
    "node": ">=18"
  }
}

```

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

```typescript
#!/usr/bin/env node

import dotenv from "dotenv";
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
  CallToolRequestSchema,
  ListResourcesRequestSchema,
  ListToolsRequestSchema,
  ReadResourceRequestSchema,
  ListPromptsRequestSchema,
  GetPromptRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";

// Deps to run as a client
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";


dotenv.config();

const URI = process.env.URI || 'https://app.marketplaceadpros.com/mcp';
const BEARER_TOKEN = process.env.BEARER_TOKEN;
const NAME = process.env.MCP_NAME || 'amazon-ads-mcp-server';

if (!URI) {
  throw new Error("URI is required")
}
// console.log("starting...")
//
let transportOptions = {};
if (BEARER_TOKEN !== undefined) {
  transportOptions = {
    requestInit: {
      headers: {
        "Authorization": `Bearer ${BEARER_TOKEN}`
      }
    }
  }
}

const transport = new StreamableHTTPClientTransport(
    new URL(`${URI}`),
    transportOptions
);
// console.log("making client...")

const client = new Client(
  {
    name: NAME,
    version: "0.1.0"
  }
);

// console.log("connecting to transport...")
await client.connect(transport);


const server = new Server(
  {
    name: NAME,
    version: "0.1.0",
  },
  {
    capabilities: {
      tools: {},
    },
  }
);

/**
 * Handler for listing resources.
 */
// server.setRequestHandler(ListResourcesRequestSchema, async () => {
//   return await client.listResources();
// });
//
// /**
//  * Handler for reading the contents of a specific resource.
//  */
// server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
//   return await client.readResource({
//     uri: request.params.uri,
//   })
//
// });

/**
 * Handler that lists available tools.
 * Exposes a single "chat" tool that lets clients chat with another AI.
 */
server.setRequestHandler(ListToolsRequestSchema, async () => {
  return await client.listTools();
});

/**
 * Handler for the chat tool.
 * Connects to an OpenAI SDK compatible AI Integration.
 */
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  return await client.callTool({
    name: request.params.name,
    arguments: request.params.arguments || {},
  });
});

/**
 * Handler that lists available prompts.
 */
// server.setRequestHandler(ListPromptsRequestSchema, async () => {
//   return await client.listPrompts();
// });
//
// /**
//  * Handler for the get prompt.
//  */
// server.setRequestHandler(GetPromptRequestSchema, async (request) => {
//   return await client.getPrompt({
//     name: request.params.name,
//   });
// });

/**
 * Start the server using stdio transport.
 * This allows the server to communicate via standard input/output streams.
 */
async function main() {
  const transport = new StdioServerTransport();
  await server.connect(transport);
}

main().catch((error) => {
  console.error("Server error:", error);
  process.exit(1);
});



```