systemprompt-mcp-interview
Version:
A specialized Model Context Protocol (MCP) server that enables AI-powered interview roleplay scenarios
157 lines • 5.6 kB
JavaScript
import { sendJsonResultNotification } from "../handlers/notifications.js";
export class SystemPromptService {
static instance = null;
apiKey;
baseUrl;
constructor() {
this.apiKey = process.env.SYSTEMPROMPT_API_KEY || "";
this.baseUrl = process.env.SYSTEMPROMPT_BASE_URL || "https://api.systemprompt.io/v1";
}
static initialize() {
SystemPromptService.instance = new SystemPromptService();
}
static getInstance() {
if (!SystemPromptService.instance) {
throw new Error("SystemPromptService must be initialized with an API key first");
}
return SystemPromptService.instance;
}
static cleanup() {
SystemPromptService.instance = null;
}
async fetch(method, path, body) {
const url = new URL(path, this.baseUrl).toString();
return fetch(url, {
method,
headers: {
"Content-Type": "application/json",
"api-key": this.apiKey,
},
body: body ? JSON.stringify(body) : undefined,
});
}
async request(method, path, body, headers) {
try {
const url = `${this.baseUrl}${path}`;
const response = await fetch(url, {
method,
headers: {
"Content-Type": "application/json",
"api-key": this.apiKey,
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache",
Expires: "0",
...headers,
},
body: body ? JSON.stringify(body) : undefined,
});
const text = await response.text();
let data;
try {
data = text ? JSON.parse(text) : undefined;
}
catch (error) {
throw new Error("Failed to parse API response");
}
if (!response.ok) {
switch (response.status) {
case 403:
throw new Error("Invalid API key");
case 404:
throw new Error("Resource not found - it may have been deleted");
case 409:
throw new Error("Resource conflict - it may have been edited");
case 400:
throw new Error("Invalid data");
default:
throw new Error(data?.message || "API request failed");
}
}
if (response.status === 204) {
return undefined;
}
return data;
}
catch (error) {
if (error instanceof Error) {
if (error.message === "Failed to fetch") {
throw new Error("API request failed");
}
throw error;
}
throw new Error("API request failed");
}
}
async getAllPrompts() {
return this.request("GET", "/prompt");
}
async createPrompt(data) {
return this.request("POST", "/prompt", data);
}
async editPrompt(uuid, data) {
return this.request("PUT", `/prompt/${uuid}`, data);
}
async deletePrompt(uuid) {
return this.request("DELETE", `/prompt/${uuid}`);
}
async createBlock(data) {
return this.request("POST", "/block", data);
}
async editBlock(uuid, data) {
return this.request("PUT", `/block/${uuid}`, data);
}
async listBlocks(options = {}) {
const params = new URLSearchParams();
if (options.tags?.length) {
params.set("tag", options.tags.join(","));
}
else {
params.set("tag", "mcp_systemprompt_interview");
}
if (options.status)
params.set("status", options.status);
if (options.search)
params.set("search", options.search);
if (options.page)
params.set("page", options.page.toString());
if (options.limit)
params.set("limit", options.limit.toString());
if (options.sortBy)
params.set("sort_by", options.sortBy);
if (options.sortDirection)
params.set("sort_direction", options.sortDirection);
const queryString = params.toString();
const url = `/block${queryString ? `?${queryString}` : ""}`;
// Add notification to log the full URL
const fullUrl = `${this.baseUrl}${url}`;
await sendJsonResultNotification(`Requesting SystemPrompt URL: ${fullUrl}`);
// Add cache-busting headers to the request
return this.request("GET", url, undefined, {
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache",
Expires: "0",
});
}
async getBlock(blockId) {
return this.request("GET", `/block/${blockId}?t=${Date.now()}`);
}
async getAgent(agentId) {
return this.request("GET", `/agent/${agentId}`);
}
async listAgents() {
return this.request("GET", "/agent");
}
async createAgent(data) {
return this.request("POST", "/agent", data);
}
async editAgent(uuid, data) {
return this.request("PUT", `/agent/${uuid}`, data);
}
async deleteBlock(uuid) {
return this.request("DELETE", `/block/${uuid}`);
}
async fetchUserStatus() {
return this.request("GET", "/user/mcp");
}
}
//# sourceMappingURL=systemprompt-service.js.map