@cyanheads/pubchem-mcp-server 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +236 -0
- package/dist/config/index.d.ts +98 -0
- package/dist/config/index.js +326 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +214 -0
- package/dist/mcp-server/server.d.ts +21 -0
- package/dist/mcp-server/server.js +124 -0
- package/dist/mcp-server/tools/fetchAssaySummary/index.d.ts +7 -0
- package/dist/mcp-server/tools/fetchAssaySummary/index.js +7 -0
- package/dist/mcp-server/tools/fetchAssaySummary/logic.d.ts +71 -0
- package/dist/mcp-server/tools/fetchAssaySummary/logic.js +96 -0
- package/dist/mcp-server/tools/fetchAssaySummary/registration.d.ts +12 -0
- package/dist/mcp-server/tools/fetchAssaySummary/registration.js +68 -0
- package/dist/mcp-server/tools/fetchCompoundProperties/index.d.ts +7 -0
- package/dist/mcp-server/tools/fetchCompoundProperties/index.js +7 -0
- package/dist/mcp-server/tools/fetchCompoundProperties/logic.d.ts +131 -0
- package/dist/mcp-server/tools/fetchCompoundProperties/logic.js +100 -0
- package/dist/mcp-server/tools/fetchCompoundProperties/registration.d.ts +13 -0
- package/dist/mcp-server/tools/fetchCompoundProperties/registration.js +52 -0
- package/dist/mcp-server/tools/fetchCompoundXrefs/index.d.ts +7 -0
- package/dist/mcp-server/tools/fetchCompoundXrefs/index.js +7 -0
- package/dist/mcp-server/tools/fetchCompoundXrefs/logic.d.ts +89 -0
- package/dist/mcp-server/tools/fetchCompoundXrefs/logic.js +145 -0
- package/dist/mcp-server/tools/fetchCompoundXrefs/registration.d.ts +13 -0
- package/dist/mcp-server/tools/fetchCompoundXrefs/registration.js +52 -0
- package/dist/mcp-server/tools/fetchSubstanceDetails/index.d.ts +7 -0
- package/dist/mcp-server/tools/fetchSubstanceDetails/index.js +7 -0
- package/dist/mcp-server/tools/fetchSubstanceDetails/logic.d.ts +69 -0
- package/dist/mcp-server/tools/fetchSubstanceDetails/logic.js +97 -0
- package/dist/mcp-server/tools/fetchSubstanceDetails/registration.d.ts +13 -0
- package/dist/mcp-server/tools/fetchSubstanceDetails/registration.js +79 -0
- package/dist/mcp-server/tools/getCompoundImage/index.d.ts +7 -0
- package/dist/mcp-server/tools/getCompoundImage/index.js +7 -0
- package/dist/mcp-server/tools/getCompoundImage/logic.d.ts +42 -0
- package/dist/mcp-server/tools/getCompoundImage/logic.js +68 -0
- package/dist/mcp-server/tools/getCompoundImage/registration.d.ts +12 -0
- package/dist/mcp-server/tools/getCompoundImage/registration.js +57 -0
- package/dist/mcp-server/tools/searchAssaysByTarget/index.d.ts +7 -0
- package/dist/mcp-server/tools/searchAssaysByTarget/index.js +7 -0
- package/dist/mcp-server/tools/searchAssaysByTarget/logic.d.ts +35 -0
- package/dist/mcp-server/tools/searchAssaysByTarget/logic.js +64 -0
- package/dist/mcp-server/tools/searchAssaysByTarget/registration.d.ts +12 -0
- package/dist/mcp-server/tools/searchAssaysByTarget/registration.js +62 -0
- package/dist/mcp-server/tools/searchCompoundByIdentifier/index.d.ts +7 -0
- package/dist/mcp-server/tools/searchCompoundByIdentifier/index.js +7 -0
- package/dist/mcp-server/tools/searchCompoundByIdentifier/logic.d.ts +35 -0
- package/dist/mcp-server/tools/searchCompoundByIdentifier/logic.js +59 -0
- package/dist/mcp-server/tools/searchCompoundByIdentifier/registration.d.ts +12 -0
- package/dist/mcp-server/tools/searchCompoundByIdentifier/registration.js +51 -0
- package/dist/mcp-server/tools/searchCompoundsByFormula/index.d.ts +7 -0
- package/dist/mcp-server/tools/searchCompoundsByFormula/index.js +7 -0
- package/dist/mcp-server/tools/searchCompoundsByFormula/logic.d.ts +38 -0
- package/dist/mcp-server/tools/searchCompoundsByFormula/logic.js +75 -0
- package/dist/mcp-server/tools/searchCompoundsByFormula/registration.d.ts +12 -0
- package/dist/mcp-server/tools/searchCompoundsByFormula/registration.js +62 -0
- package/dist/mcp-server/tools/searchCompoundsBySimilarity/index.d.ts +7 -0
- package/dist/mcp-server/tools/searchCompoundsBySimilarity/index.js +7 -0
- package/dist/mcp-server/tools/searchCompoundsBySimilarity/logic.d.ts +41 -0
- package/dist/mcp-server/tools/searchCompoundsBySimilarity/logic.js +82 -0
- package/dist/mcp-server/tools/searchCompoundsBySimilarity/registration.d.ts +12 -0
- package/dist/mcp-server/tools/searchCompoundsBySimilarity/registration.js +62 -0
- package/dist/mcp-server/tools/searchCompoundsByStructure/index.d.ts +7 -0
- package/dist/mcp-server/tools/searchCompoundsByStructure/index.js +7 -0
- package/dist/mcp-server/tools/searchCompoundsByStructure/logic.d.ts +41 -0
- package/dist/mcp-server/tools/searchCompoundsByStructure/logic.js +78 -0
- package/dist/mcp-server/tools/searchCompoundsByStructure/registration.d.ts +13 -0
- package/dist/mcp-server/tools/searchCompoundsByStructure/registration.js +63 -0
- package/dist/mcp-server/transports/auth/core/authContext.d.ts +33 -0
- package/dist/mcp-server/transports/auth/core/authContext.js +24 -0
- package/dist/mcp-server/transports/auth/core/authTypes.d.ts +17 -0
- package/dist/mcp-server/transports/auth/core/authTypes.js +5 -0
- package/dist/mcp-server/transports/auth/core/authUtils.d.ts +18 -0
- package/dist/mcp-server/transports/auth/core/authUtils.js +45 -0
- package/dist/mcp-server/transports/auth/index.d.ts +10 -0
- package/dist/mcp-server/transports/auth/index.js +9 -0
- package/dist/mcp-server/transports/auth/strategies/jwt/jwtMiddleware.d.ts +27 -0
- package/dist/mcp-server/transports/auth/strategies/jwt/jwtMiddleware.js +149 -0
- package/dist/mcp-server/transports/auth/strategies/oauth/oauthMiddleware.d.ts +20 -0
- package/dist/mcp-server/transports/auth/strategies/oauth/oauthMiddleware.js +124 -0
- package/dist/mcp-server/transports/httpErrorHandler.d.ts +26 -0
- package/dist/mcp-server/transports/httpErrorHandler.js +73 -0
- package/dist/mcp-server/transports/httpTransport.d.ts +21 -0
- package/dist/mcp-server/transports/httpTransport.js +208 -0
- package/dist/mcp-server/transports/stdioTransport.d.ts +42 -0
- package/dist/mcp-server/transports/stdioTransport.js +63 -0
- package/dist/services/pubchem/pubchemApiClient.d.ts +52 -0
- package/dist/services/pubchem/pubchemApiClient.js +120 -0
- package/dist/types-global/errors.d.ts +127 -0
- package/dist/types-global/errors.js +126 -0
- package/dist/utils/index.d.ts +12 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/internal/errorHandler.d.ts +176 -0
- package/dist/utils/internal/errorHandler.js +335 -0
- package/dist/utils/internal/index.d.ts +9 -0
- package/dist/utils/internal/index.js +9 -0
- package/dist/utils/internal/logger.d.ts +148 -0
- package/dist/utils/internal/logger.js +430 -0
- package/dist/utils/internal/requestContext.d.ts +83 -0
- package/dist/utils/internal/requestContext.js +72 -0
- package/dist/utils/metrics/index.d.ts +7 -0
- package/dist/utils/metrics/index.js +7 -0
- package/dist/utils/metrics/tokenCounter.d.ts +35 -0
- package/dist/utils/metrics/tokenCounter.js +110 -0
- package/dist/utils/network/fetchWithTimeout.d.ts +21 -0
- package/dist/utils/network/fetchWithTimeout.js +59 -0
- package/dist/utils/network/index.d.ts +6 -0
- package/dist/utils/network/index.js +5 -0
- package/dist/utils/parsing/dateParser.d.ts +73 -0
- package/dist/utils/parsing/dateParser.js +107 -0
- package/dist/utils/parsing/index.d.ts +8 -0
- package/dist/utils/parsing/index.js +8 -0
- package/dist/utils/parsing/jsonParser.d.ts +82 -0
- package/dist/utils/parsing/jsonParser.js +126 -0
- package/dist/utils/scheduling/index.d.ts +6 -0
- package/dist/utils/scheduling/index.js +6 -0
- package/dist/utils/scheduling/scheduler.d.ts +72 -0
- package/dist/utils/scheduling/scheduler.js +152 -0
- package/dist/utils/security/idGenerator.d.ts +137 -0
- package/dist/utils/security/idGenerator.js +215 -0
- package/dist/utils/security/index.d.ts +9 -0
- package/dist/utils/security/index.js +9 -0
- package/dist/utils/security/rateLimiter.d.ts +108 -0
- package/dist/utils/security/rateLimiter.js +179 -0
- package/dist/utils/security/sanitization.d.ts +184 -0
- package/dist/utils/security/sanitization.js +453 -0
- package/package.json +108 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Defines the core logic, schemas, and types for the `pubchem_search_compounds_by_formula` tool.
|
|
3
|
+
* @module src/mcp-server/tools/searchCompoundsByFormula/logic
|
|
4
|
+
*/
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { pubChemApiClient } from "../../../services/pubchem/pubchemApiClient.js";
|
|
7
|
+
import { BaseErrorCode, McpError } from "../../../types-global/errors.js";
|
|
8
|
+
import { logger } from "../../../utils/index.js";
|
|
9
|
+
// 1. Define and export the Zod schema for input validation
|
|
10
|
+
const MOLECULAR_FORMULA_REGEX = /^[A-Z][a-z]?\d*([A-Z][a-z]?\d*)*$/;
|
|
11
|
+
export const PubchemSearchCompoundsByFormulaInputSchema = z.object({
|
|
12
|
+
formula: z
|
|
13
|
+
.string()
|
|
14
|
+
.regex(MOLECULAR_FORMULA_REGEX, "Invalid molecular formula format. It must follow standard chemical notation (e.g., 'C6H12O6').")
|
|
15
|
+
.describe("A valid molecular formula to search for."),
|
|
16
|
+
allowOtherElements: z
|
|
17
|
+
.boolean()
|
|
18
|
+
.optional()
|
|
19
|
+
.default(false)
|
|
20
|
+
.describe("If true, the search will include compounds that contain the specified formula's elements plus others. Defaults to false."),
|
|
21
|
+
maxRecords: z
|
|
22
|
+
.number()
|
|
23
|
+
.int()
|
|
24
|
+
.positive()
|
|
25
|
+
.max(100, "Cannot request more than 100 records.")
|
|
26
|
+
.optional()
|
|
27
|
+
.default(50)
|
|
28
|
+
.describe("The maximum number of matching CIDs to return. Defaults to 50, with a maximum of 100."),
|
|
29
|
+
});
|
|
30
|
+
// 3. Define and export the Zod schema for the tool's output
|
|
31
|
+
export const PubchemSearchCompoundsByFormulaOutputSchema = z.object({
|
|
32
|
+
cids: z
|
|
33
|
+
.array(z.number().int())
|
|
34
|
+
.describe("A list of PubChem Compound IDs (CIDs) that match the specified molecular formula."),
|
|
35
|
+
});
|
|
36
|
+
/**
|
|
37
|
+
* Core logic for the `pubchem_search_compounds_by_formula` tool. It finds compounds
|
|
38
|
+
* that match a given molecular formula.
|
|
39
|
+
*
|
|
40
|
+
* @param {PubchemSearchCompoundsByFormulaInput} params - The validated input parameters.
|
|
41
|
+
* @param {RequestContext} context - The request context for logging and tracing.
|
|
42
|
+
* @returns {Promise<PubchemSearchCompoundsByFormulaOutput>} A promise that resolves with a list of matching CIDs.
|
|
43
|
+
* @throws {McpError} Throws a structured error if the API request fails or returns a fault.
|
|
44
|
+
*/
|
|
45
|
+
export async function pubchemSearchCompoundsByFormulaLogic(params, context) {
|
|
46
|
+
logger.debug("Processing pubchem_search_compounds_by_formula logic...", {
|
|
47
|
+
...context,
|
|
48
|
+
params,
|
|
49
|
+
});
|
|
50
|
+
const { formula, allowOtherElements, maxRecords } = params;
|
|
51
|
+
const path = `/compound/fastformula/${encodeURIComponent(formula)}/cids/JSON?AllowOtherElements=${allowOtherElements}&MaxRecords=${maxRecords}`;
|
|
52
|
+
const response = await pubChemApiClient.get(path, context);
|
|
53
|
+
logger.debug("Raw PubChem response for pubchem_search_compounds_by_formula", {
|
|
54
|
+
...context,
|
|
55
|
+
response,
|
|
56
|
+
});
|
|
57
|
+
if (response?.Fault) {
|
|
58
|
+
logger.error("PubChem API returned a fault for formula search.", {
|
|
59
|
+
...context,
|
|
60
|
+
fault: response.Fault,
|
|
61
|
+
});
|
|
62
|
+
throw new McpError(BaseErrorCode.EXTERNAL_SERVICE_ERROR, `PubChem API Fault: ${response.Fault.Message}`, { ...context, details: response.Fault.Details });
|
|
63
|
+
}
|
|
64
|
+
if (!response ||
|
|
65
|
+
!response.IdentifierList?.CID ||
|
|
66
|
+
!Array.isArray(response.IdentifierList.CID)) {
|
|
67
|
+
logger.warning("No CIDs found for the formula search, or the response format was unexpected. Returning empty list.", { ...context, formula, response });
|
|
68
|
+
return { cids: [] };
|
|
69
|
+
}
|
|
70
|
+
const result = {
|
|
71
|
+
cids: response.IdentifierList.CID,
|
|
72
|
+
};
|
|
73
|
+
logger.info(`Found ${result.cids.length} CIDs for formula '${formula}'.`, context);
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Handles the registration of the `pubchem_search_compounds_by_formula` tool.
|
|
3
|
+
* @module src/mcp-server/tools/searchCompoundsByFormula/registration
|
|
4
|
+
*/
|
|
5
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
6
|
+
/**
|
|
7
|
+
* Registers the 'pubchem_search_compounds_by_formula' tool with the MCP server.
|
|
8
|
+
* This tool finds compounds that match a given molecular formula.
|
|
9
|
+
*
|
|
10
|
+
* @param {McpServer} server - The MCP server instance to register the tool with.
|
|
11
|
+
*/
|
|
12
|
+
export declare const registerPubchemSearchCompoundsByFormulaTool: (server: McpServer) => Promise<void>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Handles the registration of the `pubchem_search_compounds_by_formula` tool.
|
|
3
|
+
* @module src/mcp-server/tools/searchCompoundsByFormula/registration
|
|
4
|
+
*/
|
|
5
|
+
import { BaseErrorCode, McpError } from "../../../types-global/errors.js";
|
|
6
|
+
import { ErrorHandler, logger, requestContextService, } from "../../../utils/index.js";
|
|
7
|
+
import { PubchemSearchCompoundsByFormulaInputSchema, pubchemSearchCompoundsByFormulaLogic, } from "./logic.js";
|
|
8
|
+
/**
|
|
9
|
+
* Registers the 'pubchem_search_compounds_by_formula' tool with the MCP server.
|
|
10
|
+
* This tool finds compounds that match a given molecular formula.
|
|
11
|
+
*
|
|
12
|
+
* @param {McpServer} server - The MCP server instance to register the tool with.
|
|
13
|
+
*/
|
|
14
|
+
export const registerPubchemSearchCompoundsByFormulaTool = async (server) => {
|
|
15
|
+
const toolName = "pubchem_search_compounds_by_formula";
|
|
16
|
+
const toolDescription = "Finds PubChem Compound IDs (CIDs) that match a given molecular formula (e.g., 'C6H12O6').";
|
|
17
|
+
server.tool(toolName, toolDescription, PubchemSearchCompoundsByFormulaInputSchema.shape, async (params, mcpContext) => {
|
|
18
|
+
const handlerContext = requestContextService.createRequestContext({
|
|
19
|
+
operation: "HandleToolRequest",
|
|
20
|
+
toolName,
|
|
21
|
+
mcpToolContext: mcpContext,
|
|
22
|
+
input: params,
|
|
23
|
+
});
|
|
24
|
+
try {
|
|
25
|
+
logger.info(`Initiating tool request for ${toolName} for formula: '${params.formula}'`, handlerContext);
|
|
26
|
+
const result = await pubchemSearchCompoundsByFormulaLogic(params, handlerContext);
|
|
27
|
+
return {
|
|
28
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
29
|
+
isError: false,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
const handledError = ErrorHandler.handleError(error, {
|
|
34
|
+
operation: "pubchemSearchCompoundsByFormulaToolHandler",
|
|
35
|
+
context: handlerContext,
|
|
36
|
+
input: params,
|
|
37
|
+
});
|
|
38
|
+
const mcpError = handledError instanceof McpError
|
|
39
|
+
? handledError
|
|
40
|
+
: new McpError(BaseErrorCode.INTERNAL_ERROR, "An unexpected error occurred while searching for compounds by formula.", { originalErrorName: handledError.name });
|
|
41
|
+
logger.error(`Error in ${toolName} handler`, {
|
|
42
|
+
...handlerContext,
|
|
43
|
+
error: mcpError,
|
|
44
|
+
});
|
|
45
|
+
return {
|
|
46
|
+
content: [
|
|
47
|
+
{
|
|
48
|
+
type: "text",
|
|
49
|
+
text: JSON.stringify({
|
|
50
|
+
error: {
|
|
51
|
+
code: mcpError.code,
|
|
52
|
+
message: mcpError.message,
|
|
53
|
+
details: mcpError.details,
|
|
54
|
+
},
|
|
55
|
+
}),
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
isError: true,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Barrel file for the `pubchem_search_compounds_by_similarity` tool.
|
|
3
|
+
* This file serves as the public interface for the tool module,
|
|
4
|
+
* primarily exporting the `registerPubchemSearchCompoundsBySimilarityTool` function.
|
|
5
|
+
* @module src/mcp-server/tools/searchCompoundsBySimilarity/index
|
|
6
|
+
*/
|
|
7
|
+
export { registerPubchemSearchCompoundsBySimilarityTool } from "./registration.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Barrel file for the `pubchem_search_compounds_by_similarity` tool.
|
|
3
|
+
* This file serves as the public interface for the tool module,
|
|
4
|
+
* primarily exporting the `registerPubchemSearchCompoundsBySimilarityTool` function.
|
|
5
|
+
* @module src/mcp-server/tools/searchCompoundsBySimilarity/index
|
|
6
|
+
*/
|
|
7
|
+
export { registerPubchemSearchCompoundsBySimilarityTool } from "./registration.js";
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Defines the core logic, schemas, and types for the `pubchem_search_compounds_by_similarity` tool.
|
|
3
|
+
* @module src/mcp-server/tools/searchCompoundsBySimilarity/logic
|
|
4
|
+
*/
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { type RequestContext } from "../../../utils/index.js";
|
|
7
|
+
export declare const PubchemSearchCompoundsBySimilarityInputSchema: z.ZodObject<{
|
|
8
|
+
query: z.ZodString;
|
|
9
|
+
queryType: z.ZodEnum<["smiles", "cid"]>;
|
|
10
|
+
threshold: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
11
|
+
maxRecords: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
12
|
+
}, "strip", z.ZodTypeAny, {
|
|
13
|
+
query: string;
|
|
14
|
+
queryType: "smiles" | "cid";
|
|
15
|
+
maxRecords: number;
|
|
16
|
+
threshold: number;
|
|
17
|
+
}, {
|
|
18
|
+
query: string;
|
|
19
|
+
queryType: "smiles" | "cid";
|
|
20
|
+
maxRecords?: number | undefined;
|
|
21
|
+
threshold?: number | undefined;
|
|
22
|
+
}>;
|
|
23
|
+
export type PubchemSearchCompoundsBySimilarityInput = z.infer<typeof PubchemSearchCompoundsBySimilarityInputSchema>;
|
|
24
|
+
export declare const PubchemSearchCompoundsBySimilarityOutputSchema: z.ZodObject<{
|
|
25
|
+
cids: z.ZodArray<z.ZodNumber, "many">;
|
|
26
|
+
}, "strip", z.ZodTypeAny, {
|
|
27
|
+
cids: number[];
|
|
28
|
+
}, {
|
|
29
|
+
cids: number[];
|
|
30
|
+
}>;
|
|
31
|
+
export type PubchemSearchCompoundsBySimilarityOutput = z.infer<typeof PubchemSearchCompoundsBySimilarityOutputSchema>;
|
|
32
|
+
/**
|
|
33
|
+
* Core logic for the `pubchem_search_compounds_by_similarity` tool. It finds compounds
|
|
34
|
+
* with a 2D structure similar to a given query compound.
|
|
35
|
+
*
|
|
36
|
+
* @param {PubchemSearchCompoundsBySimilarityInput} params - The validated input parameters.
|
|
37
|
+
* @param {RequestContext} context - The request context for logging and tracing.
|
|
38
|
+
* @returns {Promise<PubchemSearchCompoundsBySimilarityOutput>} A promise that resolves with a list of similar CIDs.
|
|
39
|
+
* @throws {McpError} Throws a structured error if the input is invalid or the API request fails.
|
|
40
|
+
*/
|
|
41
|
+
export declare function pubchemSearchCompoundsBySimilarityLogic(params: PubchemSearchCompoundsBySimilarityInput, context: RequestContext): Promise<PubchemSearchCompoundsBySimilarityOutput>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Defines the core logic, schemas, and types for the `pubchem_search_compounds_by_similarity` tool.
|
|
3
|
+
* @module src/mcp-server/tools/searchCompoundsBySimilarity/logic
|
|
4
|
+
*/
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { pubChemApiClient } from "../../../services/pubchem/pubchemApiClient.js";
|
|
7
|
+
import { BaseErrorCode, McpError } from "../../../types-global/errors.js";
|
|
8
|
+
import { logger } from "../../../utils/index.js";
|
|
9
|
+
// 1. Define and export the Zod schema for input validation
|
|
10
|
+
export const PubchemSearchCompoundsBySimilarityInputSchema = z.object({
|
|
11
|
+
query: z
|
|
12
|
+
.string()
|
|
13
|
+
.min(1, "Query cannot be empty.")
|
|
14
|
+
.describe("The query structure, provided as a SMILES string or a PubChem CID."),
|
|
15
|
+
queryType: z
|
|
16
|
+
.enum(["smiles", "cid"])
|
|
17
|
+
.describe("The format of the provided query structure ('smiles' or 'cid')."),
|
|
18
|
+
threshold: z
|
|
19
|
+
.number()
|
|
20
|
+
.min(70, "Similarity threshold must be at least 70.")
|
|
21
|
+
.max(100, "Similarity threshold cannot exceed 100.")
|
|
22
|
+
.optional()
|
|
23
|
+
.default(90)
|
|
24
|
+
.describe("The minimum Tanimoto similarity score required for a match, ranging from 70 to 100. Defaults to 90."),
|
|
25
|
+
maxRecords: z
|
|
26
|
+
.number()
|
|
27
|
+
.int()
|
|
28
|
+
.positive()
|
|
29
|
+
.max(100, "Cannot request more than 100 records.")
|
|
30
|
+
.optional()
|
|
31
|
+
.default(20)
|
|
32
|
+
.describe("The maximum number of similar CIDs to return. Defaults to 20, with a maximum of 100."),
|
|
33
|
+
});
|
|
34
|
+
// 3. Define and export the Zod schema for the tool's output
|
|
35
|
+
export const PubchemSearchCompoundsBySimilarityOutputSchema = z.object({
|
|
36
|
+
cids: z
|
|
37
|
+
.array(z.number().int())
|
|
38
|
+
.describe("A list of PubChem Compound IDs (CIDs) with structures similar to the query."),
|
|
39
|
+
});
|
|
40
|
+
/**
|
|
41
|
+
* Core logic for the `pubchem_search_compounds_by_similarity` tool. It finds compounds
|
|
42
|
+
* with a 2D structure similar to a given query compound.
|
|
43
|
+
*
|
|
44
|
+
* @param {PubchemSearchCompoundsBySimilarityInput} params - The validated input parameters.
|
|
45
|
+
* @param {RequestContext} context - The request context for logging and tracing.
|
|
46
|
+
* @returns {Promise<PubchemSearchCompoundsBySimilarityOutput>} A promise that resolves with a list of similar CIDs.
|
|
47
|
+
* @throws {McpError} Throws a structured error if the input is invalid or the API request fails.
|
|
48
|
+
*/
|
|
49
|
+
export async function pubchemSearchCompoundsBySimilarityLogic(params, context) {
|
|
50
|
+
logger.debug("Processing pubchem_search_compounds_by_similarity logic...", {
|
|
51
|
+
...context,
|
|
52
|
+
params,
|
|
53
|
+
});
|
|
54
|
+
const { query, queryType, threshold, maxRecords } = params;
|
|
55
|
+
if (queryType === "cid" && isNaN(parseInt(query, 10))) {
|
|
56
|
+
throw new McpError(BaseErrorCode.INVALID_INPUT, `Query type is 'cid' but the provided query '${query}' is not a valid number.`, { ...context, query });
|
|
57
|
+
}
|
|
58
|
+
const path = `/compound/fastsimilarity_2d/${queryType}/${encodeURIComponent(query)}/cids/JSON?Threshold=${threshold}&MaxRecords=${maxRecords}`;
|
|
59
|
+
const response = await pubChemApiClient.get(path, context);
|
|
60
|
+
logger.debug("Raw PubChem response for pubchem_search_compounds_by_similarity", {
|
|
61
|
+
...context,
|
|
62
|
+
response,
|
|
63
|
+
});
|
|
64
|
+
if (response?.Fault) {
|
|
65
|
+
logger.error("PubChem API returned a fault for similarity search.", {
|
|
66
|
+
...context,
|
|
67
|
+
fault: response.Fault,
|
|
68
|
+
});
|
|
69
|
+
throw new McpError(BaseErrorCode.EXTERNAL_SERVICE_ERROR, `PubChem API Fault: ${response.Fault.Message}`, { ...context, details: response.Fault.Details });
|
|
70
|
+
}
|
|
71
|
+
if (!response ||
|
|
72
|
+
!response.IdentifierList?.CID ||
|
|
73
|
+
!Array.isArray(response.IdentifierList.CID)) {
|
|
74
|
+
logger.warning("No similar CIDs found, or the response format was unexpected. Returning empty list.", { ...context, query, response });
|
|
75
|
+
return { cids: [] };
|
|
76
|
+
}
|
|
77
|
+
const result = {
|
|
78
|
+
cids: response.IdentifierList.CID,
|
|
79
|
+
};
|
|
80
|
+
logger.info(`Found ${result.cids.length} similar CIDs for query '${query}'.`, context);
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Handles the registration of the `pubchem_search_compounds_by_similarity` tool.
|
|
3
|
+
* @module src/mcp-server/tools/searchCompoundsBySimilarity/registration
|
|
4
|
+
*/
|
|
5
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
6
|
+
/**
|
|
7
|
+
* Registers the 'pubchem_search_compounds_by_similarity' tool with the MCP server.
|
|
8
|
+
* This tool finds compounds with a 2D structure similar to a query compound.
|
|
9
|
+
*
|
|
10
|
+
* @param {McpServer} server - The MCP server instance to register the tool with.
|
|
11
|
+
*/
|
|
12
|
+
export declare const registerPubchemSearchCompoundsBySimilarityTool: (server: McpServer) => Promise<void>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Handles the registration of the `pubchem_search_compounds_by_similarity` tool.
|
|
3
|
+
* @module src/mcp-server/tools/searchCompoundsBySimilarity/registration
|
|
4
|
+
*/
|
|
5
|
+
import { BaseErrorCode, McpError } from "../../../types-global/errors.js";
|
|
6
|
+
import { ErrorHandler, logger, requestContextService, } from "../../../utils/index.js";
|
|
7
|
+
import { PubchemSearchCompoundsBySimilarityInputSchema, pubchemSearchCompoundsBySimilarityLogic, } from "./logic.js";
|
|
8
|
+
/**
|
|
9
|
+
* Registers the 'pubchem_search_compounds_by_similarity' tool with the MCP server.
|
|
10
|
+
* This tool finds compounds with a 2D structure similar to a query compound.
|
|
11
|
+
*
|
|
12
|
+
* @param {McpServer} server - The MCP server instance to register the tool with.
|
|
13
|
+
*/
|
|
14
|
+
export const registerPubchemSearchCompoundsBySimilarityTool = async (server) => {
|
|
15
|
+
const toolName = "pubchem_search_compounds_by_similarity";
|
|
16
|
+
const toolDescription = "Finds compounds with a similar 2D structure to a query compound (provided as SMILES or CID), based on a Tanimoto similarity score.";
|
|
17
|
+
server.tool(toolName, toolDescription, PubchemSearchCompoundsBySimilarityInputSchema.shape, async (params, mcpContext) => {
|
|
18
|
+
const handlerContext = requestContextService.createRequestContext({
|
|
19
|
+
operation: "HandleToolRequest",
|
|
20
|
+
toolName,
|
|
21
|
+
mcpToolContext: mcpContext,
|
|
22
|
+
input: params,
|
|
23
|
+
});
|
|
24
|
+
try {
|
|
25
|
+
logger.info(`Initiating tool request for ${toolName} for query: '${params.query}'`, handlerContext);
|
|
26
|
+
const result = await pubchemSearchCompoundsBySimilarityLogic(params, handlerContext);
|
|
27
|
+
return {
|
|
28
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
29
|
+
isError: false,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
const handledError = ErrorHandler.handleError(error, {
|
|
34
|
+
operation: "pubchemSearchCompoundsBySimilarityToolHandler",
|
|
35
|
+
context: handlerContext,
|
|
36
|
+
input: params,
|
|
37
|
+
});
|
|
38
|
+
const mcpError = handledError instanceof McpError
|
|
39
|
+
? handledError
|
|
40
|
+
: new McpError(BaseErrorCode.INTERNAL_ERROR, "An unexpected error occurred while searching for similar compounds.", { originalErrorName: handledError.name });
|
|
41
|
+
logger.error(`Error in ${toolName} handler`, {
|
|
42
|
+
...handlerContext,
|
|
43
|
+
error: mcpError,
|
|
44
|
+
});
|
|
45
|
+
return {
|
|
46
|
+
content: [
|
|
47
|
+
{
|
|
48
|
+
type: "text",
|
|
49
|
+
text: JSON.stringify({
|
|
50
|
+
error: {
|
|
51
|
+
code: mcpError.code,
|
|
52
|
+
message: mcpError.message,
|
|
53
|
+
details: mcpError.details,
|
|
54
|
+
},
|
|
55
|
+
}),
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
isError: true,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Barrel file for the `pubchem_search_compounds_by_structure` tool.
|
|
3
|
+
* This file serves as the public interface for the tool module,
|
|
4
|
+
* primarily exporting the `registerPubchemSearchCompoundsByStructureTool` function.
|
|
5
|
+
* @module src/mcp-server/tools/searchCompoundsByStructure/index
|
|
6
|
+
*/
|
|
7
|
+
export { registerPubchemSearchCompoundsByStructureTool } from "./registration.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Barrel file for the `pubchem_search_compounds_by_structure` tool.
|
|
3
|
+
* This file serves as the public interface for the tool module,
|
|
4
|
+
* primarily exporting the `registerPubchemSearchCompoundsByStructureTool` function.
|
|
5
|
+
* @module src/mcp-server/tools/searchCompoundsByStructure/index
|
|
6
|
+
*/
|
|
7
|
+
export { registerPubchemSearchCompoundsByStructureTool } from "./registration.js";
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Defines the core logic, schemas, and types for the `pubchem_search_compounds_by_structure` tool.
|
|
3
|
+
* @module src/mcp-server/tools/searchCompoundsByStructure/logic
|
|
4
|
+
*/
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { type RequestContext } from "../../../utils/index.js";
|
|
7
|
+
export declare const PubchemSearchCompoundsByStructureInputSchema: z.ZodObject<{
|
|
8
|
+
searchType: z.ZodEnum<["substructure", "superstructure", "identity"]>;
|
|
9
|
+
query: z.ZodString;
|
|
10
|
+
queryType: z.ZodEnum<["smiles", "cid"]>;
|
|
11
|
+
maxRecords: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
12
|
+
}, "strip", z.ZodTypeAny, {
|
|
13
|
+
searchType: "substructure" | "superstructure" | "identity";
|
|
14
|
+
query: string;
|
|
15
|
+
queryType: "smiles" | "cid";
|
|
16
|
+
maxRecords: number;
|
|
17
|
+
}, {
|
|
18
|
+
searchType: "substructure" | "superstructure" | "identity";
|
|
19
|
+
query: string;
|
|
20
|
+
queryType: "smiles" | "cid";
|
|
21
|
+
maxRecords?: number | undefined;
|
|
22
|
+
}>;
|
|
23
|
+
export type PubchemSearchCompoundsByStructureInput = z.infer<typeof PubchemSearchCompoundsByStructureInputSchema>;
|
|
24
|
+
export declare const PubchemSearchCompoundsByStructureOutputSchema: z.ZodObject<{
|
|
25
|
+
cids: z.ZodArray<z.ZodNumber, "many">;
|
|
26
|
+
}, "strip", z.ZodTypeAny, {
|
|
27
|
+
cids: number[];
|
|
28
|
+
}, {
|
|
29
|
+
cids: number[];
|
|
30
|
+
}>;
|
|
31
|
+
export type PubchemSearchCompoundsByStructureOutput = z.infer<typeof PubchemSearchCompoundsByStructureOutputSchema>;
|
|
32
|
+
/**
|
|
33
|
+
* Core logic for the `pubchem_search_compounds_by_structure` tool. It performs a structural
|
|
34
|
+
* search (substructure, superstructure, or identity) based on a query structure.
|
|
35
|
+
*
|
|
36
|
+
* @param {PubchemSearchCompoundsByStructureInput} params - The validated input parameters.
|
|
37
|
+
* @param {RequestContext} context - The request context for logging and tracing.
|
|
38
|
+
* @returns {Promise<PubchemSearchCompoundsByStructureOutput>} A promise that resolves with a list of matching CIDs.
|
|
39
|
+
* @throws {McpError} Throws a structured error if the input is invalid or the API request fails.
|
|
40
|
+
*/
|
|
41
|
+
export declare function pubchemSearchCompoundsByStructureLogic(params: PubchemSearchCompoundsByStructureInput, context: RequestContext): Promise<PubchemSearchCompoundsByStructureOutput>;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Defines the core logic, schemas, and types for the `pubchem_search_compounds_by_structure` tool.
|
|
3
|
+
* @module src/mcp-server/tools/searchCompoundsByStructure/logic
|
|
4
|
+
*/
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { pubChemApiClient } from "../../../services/pubchem/pubchemApiClient.js";
|
|
7
|
+
import { BaseErrorCode, McpError } from "../../../types-global/errors.js";
|
|
8
|
+
import { logger } from "../../../utils/index.js";
|
|
9
|
+
// 1. Define and export the Zod schema for input validation
|
|
10
|
+
export const PubchemSearchCompoundsByStructureInputSchema = z.object({
|
|
11
|
+
searchType: z
|
|
12
|
+
.enum(["substructure", "superstructure", "identity"])
|
|
13
|
+
.describe("The type of structural search to perform: 'substructure' (finds molecules containing the query), 'superstructure' (finds molecules contained within the query), or 'identity' (finds exact matches)."),
|
|
14
|
+
query: z
|
|
15
|
+
.string()
|
|
16
|
+
.min(1, "Query cannot be empty.")
|
|
17
|
+
.describe("The query structure, provided as a SMILES string (e.g., 'c1ccccc1') or a PubChem CID (e.g., '2244')."),
|
|
18
|
+
queryType: z
|
|
19
|
+
.enum(["smiles", "cid"])
|
|
20
|
+
.describe("The format of the provided query structure ('smiles' or 'cid')."),
|
|
21
|
+
maxRecords: z
|
|
22
|
+
.number()
|
|
23
|
+
.int()
|
|
24
|
+
.positive()
|
|
25
|
+
.max(100, "Cannot request more than 100 records.")
|
|
26
|
+
.optional()
|
|
27
|
+
.default(20)
|
|
28
|
+
.describe("The maximum number of matching CIDs to return. Defaults to 20, with a maximum of 100."),
|
|
29
|
+
});
|
|
30
|
+
// 3. Define and export the Zod schema for the tool's output
|
|
31
|
+
export const PubchemSearchCompoundsByStructureOutputSchema = z.object({
|
|
32
|
+
cids: z
|
|
33
|
+
.array(z.number().int())
|
|
34
|
+
.describe("A list of PubChem Compound IDs (CIDs) that match the structural search criteria."),
|
|
35
|
+
});
|
|
36
|
+
/**
|
|
37
|
+
* Core logic for the `pubchem_search_compounds_by_structure` tool. It performs a structural
|
|
38
|
+
* search (substructure, superstructure, or identity) based on a query structure.
|
|
39
|
+
*
|
|
40
|
+
* @param {PubchemSearchCompoundsByStructureInput} params - The validated input parameters.
|
|
41
|
+
* @param {RequestContext} context - The request context for logging and tracing.
|
|
42
|
+
* @returns {Promise<PubchemSearchCompoundsByStructureOutput>} A promise that resolves with a list of matching CIDs.
|
|
43
|
+
* @throws {McpError} Throws a structured error if the input is invalid or the API request fails.
|
|
44
|
+
*/
|
|
45
|
+
export async function pubchemSearchCompoundsByStructureLogic(params, context) {
|
|
46
|
+
logger.debug("Processing pubchem_search_compounds_by_structure logic...", {
|
|
47
|
+
...context,
|
|
48
|
+
params,
|
|
49
|
+
});
|
|
50
|
+
const { searchType, query, queryType, maxRecords } = params;
|
|
51
|
+
if (queryType === "cid" && isNaN(parseInt(query, 10))) {
|
|
52
|
+
throw new McpError(BaseErrorCode.INVALID_INPUT, `Query type is 'cid' but the provided query '${query}' is not a valid number.`, { ...context, query });
|
|
53
|
+
}
|
|
54
|
+
const path = `/compound/fast${searchType}/${queryType}/${encodeURIComponent(query)}/cids/JSON?MaxRecords=${maxRecords}`;
|
|
55
|
+
const response = await pubChemApiClient.get(path, context);
|
|
56
|
+
logger.debug("Raw PubChem response for pubchem_search_compounds_by_structure", {
|
|
57
|
+
...context,
|
|
58
|
+
response,
|
|
59
|
+
});
|
|
60
|
+
if (response?.Fault) {
|
|
61
|
+
logger.error("PubChem API returned a fault for structure search.", {
|
|
62
|
+
...context,
|
|
63
|
+
fault: response.Fault,
|
|
64
|
+
});
|
|
65
|
+
throw new McpError(BaseErrorCode.EXTERNAL_SERVICE_ERROR, `PubChem API Fault: ${response.Fault.Message}`, { ...context, details: response.Fault.Details });
|
|
66
|
+
}
|
|
67
|
+
if (!response ||
|
|
68
|
+
!response.IdentifierList?.CID ||
|
|
69
|
+
!Array.isArray(response.IdentifierList.CID)) {
|
|
70
|
+
logger.warning("No CIDs found for the structural search, or the response format was unexpected. Returning empty list.", { ...context, query, response });
|
|
71
|
+
return { cids: [] };
|
|
72
|
+
}
|
|
73
|
+
const result = {
|
|
74
|
+
cids: response.IdentifierList.CID,
|
|
75
|
+
};
|
|
76
|
+
logger.info(`Found ${result.cids.length} CIDs for ${searchType} search with query '${query}'.`, context);
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Handles the registration of the `pubchem_search_compounds_by_structure` tool.
|
|
3
|
+
* @module src/mcp-server/tools/searchCompoundsByStructure/registration
|
|
4
|
+
*/
|
|
5
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
6
|
+
/**
|
|
7
|
+
* Registers the 'pubchem_search_compounds_by_structure' tool with the MCP server.
|
|
8
|
+
* This tool performs a structural search (substructure, superstructure, or identity)
|
|
9
|
+
* based on a query structure.
|
|
10
|
+
*
|
|
11
|
+
* @param {McpServer} server - The MCP server instance to register the tool with.
|
|
12
|
+
*/
|
|
13
|
+
export declare const registerPubchemSearchCompoundsByStructureTool: (server: McpServer) => Promise<void>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Handles the registration of the `pubchem_search_compounds_by_structure` tool.
|
|
3
|
+
* @module src/mcp-server/tools/searchCompoundsByStructure/registration
|
|
4
|
+
*/
|
|
5
|
+
import { BaseErrorCode, McpError } from "../../../types-global/errors.js";
|
|
6
|
+
import { ErrorHandler, logger, requestContextService, } from "../../../utils/index.js";
|
|
7
|
+
import { PubchemSearchCompoundsByStructureInputSchema, pubchemSearchCompoundsByStructureLogic, } from "./logic.js";
|
|
8
|
+
/**
|
|
9
|
+
* Registers the 'pubchem_search_compounds_by_structure' tool with the MCP server.
|
|
10
|
+
* This tool performs a structural search (substructure, superstructure, or identity)
|
|
11
|
+
* based on a query structure.
|
|
12
|
+
*
|
|
13
|
+
* @param {McpServer} server - The MCP server instance to register the tool with.
|
|
14
|
+
*/
|
|
15
|
+
export const registerPubchemSearchCompoundsByStructureTool = async (server) => {
|
|
16
|
+
const toolName = "pubchem_search_compounds_by_structure";
|
|
17
|
+
const toolDescription = "Performs a structural search (substructure, superstructure, or identity) using a SMILES string or a PubChem CID as the query. Essential for finding structurally related compounds.";
|
|
18
|
+
server.tool(toolName, toolDescription, PubchemSearchCompoundsByStructureInputSchema.shape, async (params, mcpContext) => {
|
|
19
|
+
const handlerContext = requestContextService.createRequestContext({
|
|
20
|
+
operation: "HandleToolRequest",
|
|
21
|
+
toolName,
|
|
22
|
+
mcpToolContext: mcpContext,
|
|
23
|
+
input: params,
|
|
24
|
+
});
|
|
25
|
+
try {
|
|
26
|
+
logger.info(`Initiating tool request for ${toolName} for query: '${params.query}'`, handlerContext);
|
|
27
|
+
const result = await pubchemSearchCompoundsByStructureLogic(params, handlerContext);
|
|
28
|
+
return {
|
|
29
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
30
|
+
isError: false,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
const handledError = ErrorHandler.handleError(error, {
|
|
35
|
+
operation: "pubchemSearchCompoundsByStructureToolHandler",
|
|
36
|
+
context: handlerContext,
|
|
37
|
+
input: params,
|
|
38
|
+
});
|
|
39
|
+
const mcpError = handledError instanceof McpError
|
|
40
|
+
? handledError
|
|
41
|
+
: new McpError(BaseErrorCode.INTERNAL_ERROR, "An unexpected error occurred while performing the structure search.", { originalErrorName: handledError.name });
|
|
42
|
+
logger.error(`Error in ${toolName} handler`, {
|
|
43
|
+
...handlerContext,
|
|
44
|
+
error: mcpError,
|
|
45
|
+
});
|
|
46
|
+
return {
|
|
47
|
+
content: [
|
|
48
|
+
{
|
|
49
|
+
type: "text",
|
|
50
|
+
text: JSON.stringify({
|
|
51
|
+
error: {
|
|
52
|
+
code: mcpError.code,
|
|
53
|
+
message: mcpError.message,
|
|
54
|
+
details: mcpError.details,
|
|
55
|
+
},
|
|
56
|
+
}),
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
isError: true,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Defines the AsyncLocalStorage context for authentication information.
|
|
3
|
+
* This module provides a mechanism to store and retrieve authentication details
|
|
4
|
+
* (like scopes and client ID) across asynchronous operations, making it available
|
|
5
|
+
* from the middleware layer down to the tool and resource handlers without
|
|
6
|
+
* drilling props.
|
|
7
|
+
*
|
|
8
|
+
* @module src/mcp-server/transports/auth/core/authContext
|
|
9
|
+
*/
|
|
10
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
11
|
+
import type { AuthInfo } from "./authTypes.js";
|
|
12
|
+
/**
|
|
13
|
+
* Defines the structure of the store used within the AsyncLocalStorage.
|
|
14
|
+
* It holds the authentication information for the current request context.
|
|
15
|
+
*/
|
|
16
|
+
interface AuthStore {
|
|
17
|
+
authInfo: AuthInfo;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* An instance of AsyncLocalStorage to hold the authentication context (`AuthStore`).
|
|
21
|
+
* This allows `authInfo` to be accessible throughout the async call chain of a request
|
|
22
|
+
* after being set in the authentication middleware.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // In middleware:
|
|
26
|
+
* await authContext.run({ authInfo }, next);
|
|
27
|
+
*
|
|
28
|
+
* // In a deeper handler:
|
|
29
|
+
* const store = authContext.getStore();
|
|
30
|
+
* const scopes = store?.authInfo.scopes;
|
|
31
|
+
*/
|
|
32
|
+
export declare const authContext: AsyncLocalStorage<AuthStore>;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Defines the AsyncLocalStorage context for authentication information.
|
|
3
|
+
* This module provides a mechanism to store and retrieve authentication details
|
|
4
|
+
* (like scopes and client ID) across asynchronous operations, making it available
|
|
5
|
+
* from the middleware layer down to the tool and resource handlers without
|
|
6
|
+
* drilling props.
|
|
7
|
+
*
|
|
8
|
+
* @module src/mcp-server/transports/auth/core/authContext
|
|
9
|
+
*/
|
|
10
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
11
|
+
/**
|
|
12
|
+
* An instance of AsyncLocalStorage to hold the authentication context (`AuthStore`).
|
|
13
|
+
* This allows `authInfo` to be accessible throughout the async call chain of a request
|
|
14
|
+
* after being set in the authentication middleware.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* // In middleware:
|
|
18
|
+
* await authContext.run({ authInfo }, next);
|
|
19
|
+
*
|
|
20
|
+
* // In a deeper handler:
|
|
21
|
+
* const store = authContext.getStore();
|
|
22
|
+
* const scopes = store?.authInfo.scopes;
|
|
23
|
+
*/
|
|
24
|
+
export const authContext = new AsyncLocalStorage();
|