Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/api-hub/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export class ApiHubClient implements Client {
prefix = "api_hub";
config = ConfigurationSchema;

async configure(_server: SmartBearMcpServer, config: z.infer<typeof ConfigurationSchema>): Promise<boolean> {
async configure(
_server: SmartBearMcpServer,
config: z.infer<typeof ConfigurationSchema>,
_cache?: any,
): Promise<boolean> {
this.api = new ApiHubAPI(
new ApiHubConfiguration({ token: config.api_key }),
`${MCP_SERVER_NAME}/${MCP_SERVER_VERSION}`,
Expand Down
31 changes: 13 additions & 18 deletions src/bugsnag/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import NodeCache from "node-cache";
import { z } from "zod";

import type { CacheService } from "../common/cache.js";
import { MCP_SERVER_NAME, MCP_SERVER_VERSION } from "../common/info.js";
import type { SmartBearMcpServer } from "../common/server.js";
import {
Expand Down Expand Up @@ -69,7 +69,7 @@ const ConfigurationSchema = z.object({
});

export class BugsnagClient implements Client {
private cache: NodeCache;
private cache?: CacheService;
private projectApiKey?: string;
private _currentUserApi: CurrentUserAPI | undefined;
private _errorsApi: ErrorAPI | undefined;
Expand Down Expand Up @@ -100,16 +100,11 @@ export class BugsnagClient implements Client {
prefix = "bugsnag";
config = ConfigurationSchema;

constructor() {
this.cache = new NodeCache({
stdTTL: 24 * 60 * 60, // default cache TTL of 24 hours
});
}

async configure(
_server: SmartBearMcpServer,
server: SmartBearMcpServer,
config: z.infer<typeof ConfigurationSchema>,
): Promise<boolean> {
this.cache = server.getCache();
this._appEndpoint = this.getEndpoint(
"app",
config.project_api_key,
Expand Down Expand Up @@ -214,15 +209,15 @@ export class BugsnagClient implements Client {
}

async getOrganization(): Promise<Organization> {
let org = this.cache.get<Organization>(cacheKeys.ORG);
let org = this.cache?.get<Organization>(cacheKeys.ORG);
if (!org) {
const response = await this.currentUserApi.listUserOrganizations();
const orgs = response.body;
if (!orgs || orgs.length === 0) {
throw new Error("No organizations found for the current user.");
}
org = orgs[0];
this.cache.set(cacheKeys.ORG, org);
this.cache?.set(cacheKeys.ORG, org);
}
return org;
}
Expand All @@ -232,14 +227,14 @@ export class BugsnagClient implements Client {
// stores them in the cache for future use.
// It throws an error if no organizations are found in the cache.
async getProjects(): Promise<Project[]> {
let projects = this.cache.get<Project[]>(cacheKeys.PROJECTS);
let projects = this.cache?.get<Project[]>(cacheKeys.PROJECTS);
if (!projects) {
const org = await this.getOrganization();
const response = await this.currentUserApi.getOrganizationProjects(
org.id,
);
projects = response.body;
this.cache.set(cacheKeys.PROJECTS, projects);
this.cache?.set(cacheKeys.PROJECTS, projects);
}
return projects;
}
Expand All @@ -250,7 +245,7 @@ export class BugsnagClient implements Client {
}

async getCurrentProject(): Promise<Project | null> {
let project = this.cache.get<Project>(cacheKeys.CURRENT_PROJECT) ?? null;
let project = this.cache?.get<Project>(cacheKeys.CURRENT_PROJECT) ?? null;
if (!project && this.projectApiKey) {
const projects = await this.getProjects();
project =
Expand All @@ -260,9 +255,9 @@ export class BugsnagClient implements Client {
"Unable to find project with the configured API key.",
);
}
this.cache.set(cacheKeys.CURRENT_PROJECT, project);
this.cache?.set(cacheKeys.CURRENT_PROJECT, project);
if (project) {
this.cache.set(
this.cache?.set(
cacheKeys.CURRENT_PROJECT_EVENT_FILTERS,
await this.getProjectEventFilters(project),
);
Expand Down Expand Up @@ -782,7 +777,7 @@ export class BugsnagClient implements Client {
// Validate filter keys against cached event fields
if (args.filters) {
const eventFields =
this.cache.get<EventField[]>(
this.cache?.get<EventField[]>(
cacheKeys.CURRENT_PROJECT_EVENT_FILTERS,
) || [];
const validKeys = new Set(eventFields.map((f) => f.displayId));
Expand Down Expand Up @@ -847,7 +842,7 @@ export class BugsnagClient implements Client {
],
},
async (_args: any, _extra: any) => {
const projectFields = this.cache.get<EventField[]>(
const projectFields = this.cache?.get<EventField[]>(
cacheKeys.CURRENT_PROJECT_EVENT_FILTERS,
);
if (!projectFields)
Expand Down
71 changes: 71 additions & 0 deletions src/common/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import NodeCache from "node-cache";

/**
* Common cache service that can be shared across all clients.
* Wraps NodeCache and provides a way to disable caching entirely.
* Reads CACHE_ENABLED and CACHE_TTL environment variables for configuration.
*/
export class CacheService {
private cache: NodeCache | null;
private enabled: boolean;

constructor() {
// Read configuration from environment variables
this.enabled = process.env.CACHE_ENABLED !== "false";
const ttl = process.env.CACHE_TTL
? Number.parseInt(process.env.CACHE_TTL, 10)
: 86400; // Default 24 hours

this.cache = this.enabled
? new NodeCache({
stdTTL: ttl,
})
: null;
}

/**
* Get a value from the cache
*/
get<T>(key: string): T | undefined {
if (!this.enabled || !this.cache) {
return undefined;
}
return this.cache.get<T>(key);
}

/**
* Set a value in the cache
*/
set<T>(key: string, value: T): boolean {
if (!this.enabled || !this.cache) {
return false;
}
return this.cache.set(key, value);
}

/**
* Delete a value from the cache
*/
del(key: string): number {
if (!this.enabled || !this.cache) {
return 0;
}
return this.cache.del(key);
}

/**
* Check if caching is enabled
*/
isEnabled(): boolean {
return this.enabled;
}

/**
* Clear all cache entries
*/
flushAll(): void {
if (this.enabled && this.cache) {
this.cache.flushAll();
}
}
}
8 changes: 8 additions & 0 deletions src/common/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import {
ZodUnion,
} from "zod";
import Bugsnag from "../common/bugsnag.js";
import { CacheService } from "./cache.js";
import { MCP_SERVER_NAME, MCP_SERVER_VERSION } from "./info.js";
import { type Client, ToolError, type ToolParams } from "./types.js";

export class SmartBearMcpServer extends McpServer {
private cache: CacheService;

constructor() {
super(
{
Expand All @@ -39,6 +42,11 @@ export class SmartBearMcpServer extends McpServer {
},
},
);
this.cache = new CacheService();
}

getCache(): CacheService {
return this.cache;
}

addClient(client: Client): void {
Expand Down
2 changes: 1 addition & 1 deletion src/common/transport-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ async function handleLegacySseRequest(
}

/**
* Handle legacy SSE message POST requests
* Handle legacy POST message requests
*/
async function handleLegacyMessageRequest(
req: IncomingMessage,
Expand Down
3 changes: 3 additions & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ export interface Client {
prefix: string;
/** Zod schema defining configuration fields for this client */
config: ZodObject<ZodRawShape>;
/**
* Configure the client with the given server and configuration
*/
configure: (server: SmartBearMcpServer, config: any) => Promise<boolean>;
registerTools(
register: RegisterToolsFunction,
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ if (McpServerBugsnagAPIKey) {
}

async function main() {

// Determine transport mode from environment variable
// MCP_TRANSPORT can be "stdio" (default) or "http"
const transportMode = process.env.MCP_TRANSPORT?.toLowerCase() || "stdio";
Expand Down
5 changes: 3 additions & 2 deletions src/qmetry/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class QmetryClient implements Client {
async configure(
_server: any,
config: z.infer<typeof ConfigurationSchema>,
_cache?: any,
): Promise<boolean> {
this.token = config.api_key;
if (config.base_url) {
Expand Down Expand Up @@ -116,8 +117,8 @@ export class QmetryClient implements Client {
} catch (err) {
throw new Error(
`Failed to auto-resolve viewId/folderPath/folderID for ${autoResolveConfig.moduleName} in project ${projectKey}. ` +
`Please provide them manually or check project access. ` +
`Error: ${err instanceof Error ? err.message : String(err)}`,
`Please provide them manually or check project access. ` +
`Error: ${err instanceof Error ? err.message : String(err)}`,
);
}

Expand Down
8 changes: 6 additions & 2 deletions src/reflect/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface testExecutionArgs {
}

const ConfigurationSchema = z.object({
api_token: z.string().describe("Reflect API authentication token"),
api_token: z.string().describe("Reflect API authentication token"),
});

// ReflectClient class implementing the Client interface
Expand All @@ -41,7 +41,11 @@ export class ReflectClient implements Client {

config = ConfigurationSchema;

async configure(_server: SmartBearMcpServer, config: z.infer<typeof ConfigurationSchema>): Promise<boolean> {
async configure(
_server: SmartBearMcpServer,
config: z.infer<typeof ConfigurationSchema>,
_cache?: any,
): Promise<boolean> {
this.headers = {
"X-API-KEY": `${config.api_token}`,
"Content-Type": "application/json",
Expand Down
Loading