@gavdi/cap-mcp 0.9.4 → 0.9.7
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/README.md +36 -1
- package/cds-plugin.js +1 -1
- package/lib/annotations/constants.js +2 -0
- package/lib/annotations/parser.js +11 -5
- package/lib/annotations/structures.js +10 -1
- package/lib/config/loader.js +7 -0
- package/lib/logger.js +48 -2
- package/lib/mcp/describe-model.js +103 -0
- package/lib/mcp/entity-tools.js +548 -0
- package/lib/mcp/factory.js +14 -0
- package/lib/mcp/resources.js +18 -3
- package/lib/mcp/session-manager.js +13 -2
- package/lib/mcp/tools.js +10 -18
- package/lib/mcp/utils.js +56 -0
- package/lib/mcp.js +23 -5
- package/package.json +11 -6
- package/lib/annotations.js +0 -257
- package/lib/auth/adapter.js +0 -2
- package/lib/auth/mock.js +0 -2
- package/lib/auth/types.js +0 -2
- package/lib/mcp/customResourceTemplate.js +0 -156
- package/lib/types.js +0 -2
- package/lib/utils.js +0 -136
package/lib/utils.js
DELETED
@@ -1,136 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.MCP_SESSION_HEADER = exports.LOGGER = void 0;
|
4
|
-
exports.createMcpServer = createMcpServer;
|
5
|
-
exports.handleMcpSessionRequest = handleMcpSessionRequest;
|
6
|
-
exports.parseEntityElements = parseEntityElements;
|
7
|
-
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
8
|
-
const zod_1 = require("zod");
|
9
|
-
const structures_1 = require("./annotations/structures");
|
10
|
-
/* @ts-ignore */
|
11
|
-
const cds = global.cds || require("@sap/cds"); // This is a work around for missing cds context
|
12
|
-
exports.LOGGER = cds.log("cds-mcp");
|
13
|
-
exports.MCP_SESSION_HEADER = "mcp-session-id";
|
14
|
-
function createMcpServer(annotations) {
|
15
|
-
const packageInfo = require("../package.json");
|
16
|
-
const server = new mcp_js_1.McpServer({
|
17
|
-
name: packageInfo.name,
|
18
|
-
version: packageInfo.version,
|
19
|
-
capabilities: {
|
20
|
-
tools: { listChanged: true },
|
21
|
-
resources: { listChanged: true },
|
22
|
-
prompts: { listChanged: true },
|
23
|
-
},
|
24
|
-
});
|
25
|
-
exports.LOGGER.debug("Annotations found for server = ", annotations);
|
26
|
-
if (!annotations)
|
27
|
-
return server;
|
28
|
-
// TODO: Handle the parsed annotations
|
29
|
-
// TODO: Error handling
|
30
|
-
// TODO: This should only be mapped once, not per each server instance. Maybe this should be pre-packaged on load?
|
31
|
-
for (const [_, v] of annotations.entries()) {
|
32
|
-
switch (v.constructor) {
|
33
|
-
case structures_1.McpToolAnnotation:
|
34
|
-
const model = v;
|
35
|
-
const parameters = buildParameters(model.parameters);
|
36
|
-
exports.LOGGER.debug("Adding tool", model);
|
37
|
-
if (model.entityKey) {
|
38
|
-
const keys = buildParameters(model.keyTypeMap);
|
39
|
-
server.tool(model.name, { ...keys, ...parameters }, async (data) => {
|
40
|
-
const service = cds.services[model.serviceName];
|
41
|
-
const received = data;
|
42
|
-
const receivedKeys = {};
|
43
|
-
const receivedParams = {};
|
44
|
-
for (const [k, v] of Object.entries(received)) {
|
45
|
-
if (model.keyTypeMap?.has(k)) {
|
46
|
-
receivedKeys[k] = v;
|
47
|
-
}
|
48
|
-
if (!model.parameters?.has(k))
|
49
|
-
continue;
|
50
|
-
receivedParams[k] = v;
|
51
|
-
}
|
52
|
-
const response = await service.send({
|
53
|
-
event: model.target,
|
54
|
-
entity: model.entityKey,
|
55
|
-
data: receivedParams,
|
56
|
-
params: [receivedKeys],
|
57
|
-
});
|
58
|
-
return {
|
59
|
-
content: Array.isArray(response)
|
60
|
-
? response.map((el) => ({ type: "text", text: String(el) }))
|
61
|
-
: [{ type: "text", text: String(response) }], // TODO: This should be dynamic based on the return type
|
62
|
-
};
|
63
|
-
});
|
64
|
-
continue;
|
65
|
-
}
|
66
|
-
server.tool(model.name, parameters, async (data) => {
|
67
|
-
exports.LOGGER.debug("Tool call received, targeting service: ", model.serviceName, model.target);
|
68
|
-
const service = cds.services[model.serviceName];
|
69
|
-
const response = await service.send(model.target, data);
|
70
|
-
exports.LOGGER.debug("MCP Tool response received and being packaged");
|
71
|
-
return {
|
72
|
-
content: Array.isArray(response)
|
73
|
-
? response.map((el) => ({ type: "text", text: String(el) }))
|
74
|
-
: [{ type: "text", text: String(response) }], // TODO: This should be dynamic based on the return type
|
75
|
-
};
|
76
|
-
});
|
77
|
-
continue;
|
78
|
-
case structures_1.McpResourceAnnotation:
|
79
|
-
exports.LOGGER.debug("This is a resource");
|
80
|
-
continue;
|
81
|
-
case structures_1.McpPromptAnnotation:
|
82
|
-
exports.LOGGER.debug("This is a prompt");
|
83
|
-
continue;
|
84
|
-
default:
|
85
|
-
exports.LOGGER.error("Invalid annotation data type");
|
86
|
-
throw new Error("Invalid annotation");
|
87
|
-
}
|
88
|
-
}
|
89
|
-
return server;
|
90
|
-
}
|
91
|
-
async function handleMcpSessionRequest(req, res, sessions) {
|
92
|
-
const sessionIdHeader = req.headers[exports.MCP_SESSION_HEADER];
|
93
|
-
if (!sessionIdHeader || !sessions.has(sessionIdHeader)) {
|
94
|
-
res.status(400).send("Invalid or missing session ID");
|
95
|
-
return;
|
96
|
-
}
|
97
|
-
const session = sessions.get(sessionIdHeader);
|
98
|
-
if (!session) {
|
99
|
-
res.status(400).send("Invalid session");
|
100
|
-
return;
|
101
|
-
}
|
102
|
-
await session.transport.handleRequest(req, res);
|
103
|
-
}
|
104
|
-
function parseEntityElements(entity) {
|
105
|
-
const elements = entity.elements;
|
106
|
-
if (!elements) {
|
107
|
-
exports.LOGGER.error("Invalid object - cannot be parsed", entity);
|
108
|
-
throw new Error("Failed to parse entity object");
|
109
|
-
}
|
110
|
-
const result = new Map();
|
111
|
-
for (const el of elements) {
|
112
|
-
if (!el.type)
|
113
|
-
continue;
|
114
|
-
result.set(el.name, el.type?.replace("cds.", ""));
|
115
|
-
}
|
116
|
-
return result;
|
117
|
-
}
|
118
|
-
function buildParameters(params) {
|
119
|
-
if (!params || params.size <= 0)
|
120
|
-
return {};
|
121
|
-
const result = {};
|
122
|
-
for (const [k, v] of params.entries()) {
|
123
|
-
result[k] = determineParameterType(v);
|
124
|
-
}
|
125
|
-
return result;
|
126
|
-
}
|
127
|
-
function determineParameterType(paramType) {
|
128
|
-
switch (paramType) {
|
129
|
-
case "String":
|
130
|
-
return zod_1.z.string();
|
131
|
-
case "Integer":
|
132
|
-
return zod_1.z.number();
|
133
|
-
default:
|
134
|
-
return zod_1.z.number();
|
135
|
-
}
|
136
|
-
}
|