UNPKG

@mseep/adspirer-mcp-server

Version:

MCP server for Amazon Advertising data integration with Claude

11 lines (10 loc) 5.84 kB
#!/usr/bin/env node import{McpServer as f}from"@modelcontextprotocol/sdk/server/mcp.js";import{StdioServerTransport as E}from"@modelcontextprotocol/sdk/server/stdio.js";import _ from"dotenv";import{createClient as p}from"@supabase/supabase-js";import*as d from"dotenv";import v from"fs";d.config();var m=e=>{if(process.env.DEBUG==="true"){let n=`${new Date().toISOString()} - ${e} `;try{v.createWriteStream("mcp-debug.log",{flags:"a"}).write(n),console.error(n)}catch(o){console.error(`Error writing to log: ${o.message}`)}}},y=process.env.SUPABASE_URL,g=process.env.SUPABASE_KEY;m("Supabase client initialization complete");var s=p(y,g,{auth:{persistSession:!1}});async function u(e){try{let{data:t,error:n}=await s.from("api_keys").select("advertiser_id, active").eq("key",e).single();if(!n&&t)return{valid:t.active===!0,advertiserId:t.advertiser_id,error:t.active!==!0?"API key is inactive":void 0};let{data:o,error:a}=await s.from("api_keys").select("id, user_id").eq("key_value",e).eq("is_active",!0).single();if(a)return{valid:!1,error:"Invalid API key"};if(!o)return{valid:!1,error:"API key not found"};if(o.user_id){let{data:c,error:l}=await s.from("advertisers").select("id").eq("user_id",o.user_id).limit(1).single();if(!l&&c)return{valid:!0,advertiserId:c.id}}return{valid:!0,advertiserId:o.id}}catch(t){return console.error("Error validating API key:",t),{valid:!1,error:"Error validating API key"}}}import A from"fs";import{z as I}from"zod";_.config();var S=A.createWriteStream("mcp-debug.log",{flags:"a"});function r(e){let n=`${new Date().toISOString()} - ${e} `;S.write(n),console.error(n)}r("Starting Amazon Advertising MCP Server...");r(`MCP Server Version: ${process.env.MCP_SERVER_VERSION||"dev"}`);r(`Received API_KEY: ${process.env.API_KEY?"YES (set)":"NO (not set)"}`);r(`Environment variables: ${JSON.stringify(process.env,(e,t)=>e.toUpperCase().includes("KEY")||e.toUpperCase().includes("SECRET")?t?"[SET]":"[NOT SET]":t,2)}`);var i=new f({name:"Amazon Advertising",version:process.env.MCP_SERVER_VERSION||"1.0.0"});i.tool("getAdvertiserInfo",{},async()=>{var e,t;r("Received request for getAdvertiserInfo");try{let{data:n,error:o}=await s.from("advertisers").select("*").limit(1);if(o||!n||n.length===0)return r(`Error or no advertisers found: ${o?o.message:"No advertisers"}`),{content:[{type:"text",text:"Error: No advertiser accounts found"}],isError:!0};let a=n[0];return r(`Found advertiser: ${a.account_name}`),{content:[{type:"text",text:JSON.stringify({id:a.id,name:a.account_name,marketplace:a.marketplace,accountType:a.account_type,profileId:a.profile_id,countryCode:((e=a.metadata)==null?void 0:e.countryCode)||"Unknown",currencyCode:((t=a.metadata)==null?void 0:t.currencyCode)||"USD"},null,2)}]}}catch(n){return r(`Error getting advertiser info: ${n.message}`),{content:[{type:"text",text:`Error: ${n.message}`}],isError:!0}}});i.tool("listAdvertiserAccounts",{},async()=>{r("Received request for listAdvertiserAccounts");try{let{data:e,error:t}=await s.from("advertisers").select("id, account_name, marketplace, account_type, metadata").order("account_name",{ascending:!0});if(t)return r(`Error fetching advertisers: ${t.message}`),{content:[{type:"text",text:`Error: ${t.message}`}],isError:!0};if(!e||e.length===0)return r("No advertisers found"),{content:[{type:"text",text:"No advertiser accounts were found in the database"}]};let n=e.map(o=>{var a,c;return{id:o.id,accountName:o.account_name,marketplace:o.marketplace,accountType:o.account_type,countryCode:((a=o.metadata)==null?void 0:a.countryCode)||"Unknown",currencyCode:((c=o.metadata)==null?void 0:c.currencyCode)||"USD"}});return r(`Found ${n.length} advertisers`),{content:[{type:"text",text:JSON.stringify(n,null,2)}]}}catch(e){return r(`Error listing advertiser accounts: ${e.message}`),{content:[{type:"text",text:`Error: ${e.message}`}],isError:!0}}});i.tool("ping",{},async()=>(r("Received ping request - testing connection"),{content:[{type:"text",text:`Server is running correctly! Time: ${new Date().toISOString()} Server name: Amazon Advertising Server version: ${process.env.MCP_SERVER_VERSION||"1.0.0"}`}]}));i.tool("echo",{message:I.string()},async({message:e})=>(r(`Received echo request with message: ${e}`),{content:[{type:"text",text:`You said: ${e}`}]}));i.tool("validateApiKey",{},async()=>{r("Received request to validate API key");let e=process.env.API_KEY;if(!e)return r("No API key provided in environment variables"),{content:[{type:"text",text:"Error: No API_KEY provided in environment variables."}],isError:!0};try{r(`Attempting to validate API key: ${e.substring(0,8)}...`);let{valid:t,advertiserId:n,error:o}=await u(e);return t?(r(`API key validated successfully for advertiser ID: ${n}`),{content:[{type:"text",text:`API key is valid for advertiser ID: ${n}`}]}):(r(`API key validation failed: ${o}`),{content:[{type:"text",text:`API key validation failed: ${o||"Unknown error"}`}],isError:!0})}catch(t){return r(`Error validating API key: ${t.message}`),{content:[{type:"text",text:`Error validating API key: ${t.message}. This could be due to missing database credentials.`}],isError:!0}}});r("Starting MCP server with stdio transport");var k=new E;try{r("Attempting to connect MCP server to transport"),i.connect(k).then(()=>{r("MCP server successfully connected to transport and ready to handle requests from Claude Desktop"),process.on("SIGINT",()=>{r("Received SIGINT, shutting down server..."),process.exit(0)})}).catch(e=>{r(`Error connecting MCP server to transport: ${e.message} ${e.stack}`)})}catch(e){r(`Critical error starting MCP server: ${e.message} ${e.stack}`)}setInterval(()=>{r("MCP server heartbeat - still running")},6e4);process.on("uncaughtException",e=>{r(`Uncaught exception: ${e.message} ${e.stack}`)});process.on("unhandledRejection",(e,t)=>{r(`Unhandled promise rejection: ${e}`)});