@chinchillaenterprises/mcp-upwork
Version:
Modular Upwork MCP server for job search and proposal management via Upwork API
79 lines • 3.09 kB
JavaScript
import axios from "axios";
import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js";
export class UpworkClient {
static instance;
client;
apiKey;
apiSecret;
accessToken;
constructor() {
// Get credentials from environment variables
this.apiKey = process.env.UPWORK_API_KEY || "";
this.apiSecret = process.env.UPWORK_API_SECRET || "";
this.accessToken = process.env.UPWORK_ACCESS_TOKEN || "";
if (!this.apiKey || !this.apiSecret) {
throw new Error("UPWORK_API_KEY and UPWORK_API_SECRET environment variables are required");
}
// Initialize Upwork API client
this.client = axios.create({
baseURL: "https://www.upwork.com/api",
timeout: 30000,
headers: {
"Authorization": `Bearer ${this.accessToken}`,
"Content-Type": "application/json",
"User-Agent": "ChillMCP-Upwork/1.0.0"
},
});
}
getClient() {
return this.client;
}
static getInstance() {
if (!UpworkClient.instance) {
UpworkClient.instance = new UpworkClient();
}
return UpworkClient.instance;
}
async get(endpoint, params) {
try {
const response = await this.client.get(endpoint, { params });
return response.data;
}
catch (error) {
throw this.handleError(error);
}
}
async post(endpoint, data) {
try {
const response = await this.client.post(endpoint, data);
return response.data;
}
catch (error) {
throw this.handleError(error);
}
}
handleError(error) {
if (axios.isAxiosError(error)) {
const axiosError = error;
if (axiosError.response) {
const status = axiosError.response.status;
const data = axiosError.response.data;
switch (status) {
case 401:
return new McpError(ErrorCode.InvalidRequest, "Authentication failed. Please check your API credentials.");
case 403:
return new McpError(ErrorCode.InvalidRequest, "Access forbidden. Please check your API permissions.");
case 404:
return new McpError(ErrorCode.InvalidRequest, "Resource not found.");
case 429:
return new McpError(ErrorCode.InvalidRequest, "Rate limit exceeded. Please try again later.");
default:
return new McpError(ErrorCode.InternalError, `Upwork API error: ${data?.message || axiosError.message}`);
}
}
return new McpError(ErrorCode.InternalError, `Network error: ${axiosError.message}`);
}
return new McpError(ErrorCode.InternalError, `Unexpected error: ${error instanceof Error ? error.message : String(error)}`);
}
}
//# sourceMappingURL=upwork-client.js.map