@sassoftware/sas-score-mcp-serverjs 0.3.18 → 0.4.0

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.
Files changed (47) hide show
  1. package/cli.js +141 -26
  2. package/package.json +4 -3
  3. package/skills/mcp-tool-description-optimizer/SKILL.md +129 -0
  4. package/skills/mcp-tool-description-optimizer/references/examples.md +123 -0
  5. package/skills/sas-read-and-score/SKILL.md +91 -0
  6. package/skills/sas-read-strategy/SKILL.md +143 -0
  7. package/skills/sas-score-workflow/SKILL.md +282 -0
  8. package/src/createMcpServer.js +1 -0
  9. package/src/expressMcpServer.js +68 -28
  10. package/src/handleGetDelete.js +6 -3
  11. package/src/hapiMcpServer.js +30 -0
  12. package/src/openAPIJson.js +175 -175
  13. package/src/toolHelpers/_jobSubmit.js +2 -0
  14. package/src/toolHelpers/_listLibrary.js +56 -39
  15. package/src/toolHelpers/getLogonPayload.js +9 -7
  16. package/src/toolHelpers/getStoreOpts.js +1 -2
  17. package/src/toolHelpers/getToken.js +0 -1
  18. package/src/toolHelpers/refreshToken.js +48 -45
  19. package/src/toolHelpers/refreshTokenOauth.js +2 -2
  20. package/src/toolHelpers/tlogon.js +9 -0
  21. package/src/toolSet/devaScore.js +30 -38
  22. package/src/toolSet/findJob.js +23 -49
  23. package/src/toolSet/findJobdef.js +24 -54
  24. package/src/toolSet/findLibrary.js +25 -57
  25. package/src/toolSet/findModel.js +31 -53
  26. package/src/toolSet/findTable.js +25 -54
  27. package/src/toolSet/getEnv.js +20 -38
  28. package/src/toolSet/listJobdefs.js +24 -58
  29. package/src/toolSet/listJobs.js +24 -72
  30. package/src/toolSet/listLibraries.js +37 -47
  31. package/src/toolSet/listModels.js +20 -47
  32. package/src/toolSet/listTables.js +29 -58
  33. package/src/toolSet/makeTools.js +3 -0
  34. package/src/toolSet/modelInfo.js +18 -49
  35. package/src/toolSet/modelScore.js +27 -69
  36. package/src/toolSet/readTable.js +25 -62
  37. package/src/toolSet/runCasProgram.js +23 -43
  38. package/src/toolSet/runJob.js +20 -19
  39. package/src/toolSet/runJobdef.js +21 -23
  40. package/src/toolSet/runMacro.js +20 -20
  41. package/src/toolSet/runProgram.js +24 -71
  42. package/src/toolSet/sasQuery.js +23 -70
  43. package/src/toolSet/scrInfo.js +3 -4
  44. package/src/toolSet/setContext.js +22 -48
  45. package/src/toolSet/tableInfo.js +28 -71
  46. package/src/toolHelpers/getOpts.js +0 -51
  47. package/src/toolHelpers/getOptsViya.js +0 -44
@@ -0,0 +1,143 @@
1
+ ---
2
+ name: sas-read-strategy
3
+ description: >
4
+ Guide the user in choosing the right data retrieval tool: read-table (for raw row access with filters)
5
+ or sas-query (for analytical queries, aggregations, joins). Use this skill when the user wants to
6
+ fetch records from a SAS/CAS table. Trigger phrases include: "read records from", "get data where",
7
+ "fetch rows from", "query the table", "give me the first N records", "aggregate by", "join tables",
8
+ or any request that starts with data retrieval.
9
+ ---
10
+
11
+ # SAS Read Strategy
12
+
13
+ Guides the decision between `read-table` and `sas-query` based on the user's intent and the nature
14
+ of the data operation.
15
+
16
+ ---
17
+
18
+ ## Determine the server location
19
+
20
+ Before retrieving data, check which server(s) contain the table:
21
+
22
+ 1. **Check both servers**: Use `find-table` to check if the table exists in CAS and/or SAS
23
+ 2. **Decide server placement**:
24
+ - If table exists **only in CAS** → set `server: "cas"`
25
+ - If table exists **only in SAS** → set `server: "sas"`
26
+ - If table exists **in both** → ask the user: *"The table exists in both CAS and SAS. Which server would you prefer to query from?"*
27
+ - If table exists **in neither** → inform user and ask which library to search in
28
+
29
+ ---
30
+
31
+ ## Determine the read strategy
32
+
33
+ Ask yourself: does the user already have the data in hand?
34
+
35
+ - **Yes (user pasted values, or data is already in context)** → skip this strategy; data is ready to use.
36
+ - **No** → choose the read tool based on intent:
37
+
38
+ | User's Intent | Tool | Example |
39
+ |---|---|---|
40
+ | Get specific raw rows, apply simple filter, retrieve first N records | `read-table` | "Show me 10 rows from customers where status='active'" |
41
+ | Aggregate/summarize, calculate, join tables, analytical question | `sas-query` | "Average salary by department", "Count orders by region" |
42
+
43
+ ---
44
+
45
+ ## Using read-table
46
+
47
+ **When:**
48
+ - User asks for raw records, row-by-row data
49
+ - Simple WHERE filtering (e.g., "where status = 'active'")
50
+ - Pagination needed ("first 50 rows", "next 10 rows")
51
+
52
+ **How:**
53
+ ```
54
+ read-table({
55
+ table: "tablename",
56
+ lib: "libraryname",
57
+ server: "cas" or "sas", // determined from find-table check
58
+ limit: N, // default 10, adjust based on user request
59
+ where: "..." // optional SQL WHERE clause
60
+ })
61
+ ```
62
+
63
+ **Rules:**
64
+ - Always determine the server first using `find-table`
65
+ - Keep batch size ≤ 50 rows unless the user explicitly requests more
66
+ - If table name is missing, ask: *"Which table should I read from? (format: lib.tablename)"*
67
+ - If library is missing, ask: *"Which library contains the table?"*
68
+ - If table exists in both servers, ask user which to use
69
+ - Return raw column values; do not transform or aggregate
70
+
71
+ ---
72
+
73
+ ## Using sas-query
74
+
75
+ **When:**
76
+ - User asks for aggregation (SUM, AVG, COUNT, GROUP BY)
77
+ - User asks for joins, calculations, or analytical insights
78
+ - User's question is phrased analytically ("compare", "analyze", "breakdown", "trend")
79
+
80
+ **How:**
81
+ ```
82
+ sas-query({
83
+ table: "lib.tablename",
84
+ query: "user's natural language question",
85
+ sql: "SELECT ... FROM ... WHERE ... GROUP BY ..." // generated from query
86
+ })
87
+ ```
88
+
89
+ **Rules:**
90
+ - Check which server(s) contain the table using `find-table` first
91
+ - If table exists in both CAS and SAS, ask user which to query from
92
+ - Parse the user's natural language question into a PROC SQL SELECT statement
93
+ - Ensure SELECT statement is valid SQL syntax
94
+ - Do not add trailing semicolons to the SQL string
95
+ - If table name is missing, ask: *"Which table should I query? (format: lib.tablename)"*
96
+ - If the intent is unclear, ask for clarification: *"Do you want raw rows, or an aggregated summary?"*
97
+
98
+ ---
99
+
100
+ ## Common patterns
101
+
102
+ **Pattern A — Raw row retrieval**
103
+ > "Show me the first 5 rows from Public.customers"
104
+
105
+ → `read-table({ table: "customers", lib: "Public", limit: 5 })`
106
+
107
+ **Pattern B — Filtered retrieval**
108
+ > "Get all high-value orders (amount > 5000) from mylib.orders"
109
+
110
+ → `read-table({ table: "orders", lib: "mylib", where: "amount > 5000" })`
111
+
112
+ **Pattern C — Aggregation**
113
+ > "What is the average price by make in Public.cars?"
114
+
115
+ → `sas-query({ table: "Public.cars", query: "average price by make", sql: "SELECT make, AVG(msrp) AS avg_price FROM Public.cars GROUP BY make" })`
116
+
117
+ **Pattern D — Join + analysis**
118
+ > "Show me total sales by customer in the sales and customers tables"
119
+
120
+ → `sas-query()` with a JOIN in the generated SQL
121
+
122
+ ---
123
+
124
+ ## Error handling
125
+
126
+ | Problem | Action |
127
+ |---|---|
128
+ | Table not found in either server | Ask: *"Which library contains the table? (e.g., Public, Samples, mylib)"* |
129
+ | Table exists in both CAS and SAS | Ask: *"The table exists in both servers. Which would you prefer: CAS or SAS?"* |
130
+ | Table exists only in one server | Use that server automatically in your request |
131
+ | Library not found | Ask: *"Which library contains the table? (e.g., Public, Samples, mylib)"* |
132
+ | Ambiguous intent (raw vs aggregate) | Ask: *"Do you want individual rows or a summary by some field?"* |
133
+ | Empty result | Inform user, ask to adjust filter or query |
134
+
135
+ ---
136
+
137
+ ## Next steps
138
+
139
+ Once data is retrieved, decide the next action based on context:
140
+ - If user wants to **score** the data → move to `sas-score-workflow`
141
+ - If user wants to **visualize** → present as table or chart
142
+ - If user wants to **export** → format and offer download
143
+ - If user wants to **analyze further** → ask clarifying questions
@@ -0,0 +1,282 @@
1
+ ---
2
+ name: sas-score-workflow
3
+ description: >
4
+ Guide the full model scoring workflow: validate model familiarity, route to appropriate scoring tool
5
+ based on model type, invoke scoring with scenario data, and present merged results. Use this skill
6
+ when the user wants to run predictions on data (already fetched or user-supplied). Supports generic
7
+ syntax: "score with model <name>.<type> scenario =<params>" where type is job|jobdef|mas|scr|sas.
8
+ Trigger phrases: "score these records", "predict using model", "run model on", "score with model X.mas".
9
+ ---
10
+
11
+ # SAS Score Workflow
12
+
13
+ Orchestrates model validation, type-based routing, scoring invocation, and result presentation.
14
+ Handles both MAS models and alternative scoring engines (jobs, jobdefs, SCR, SAS programs).
15
+
16
+ ---
17
+
18
+ ## Generic Scoring Syntax
19
+
20
+ Users can invoke scoring with a unified syntax that automatically routes to the correct tool:
21
+
22
+ ```
23
+ score with model <name>.<type> [scenario =<key=value pairs>]
24
+ score <name>.<type> [scenario =<key=value pairs>]
25
+ ```
26
+
27
+ **Type determines the routing:**
28
+ - `.job` → route to `run-job` with scoring parameters
29
+ - `.jobdef` → route to `run-jobdef` with scoring parameters
30
+ - `.mas` → route to `model-score` (Model Analytical Service — default)
31
+ - `.scr` → route to `scr-score` (SAS Container Runtime)
32
+ - `.sas` → route to `run-sas-program` to run sas program in folder
33
+
34
+ If no type is specified (bare model name), assume `.mas` (MAS model).
35
+
36
+ ---
37
+
38
+ ## Type-Based Routing
39
+
40
+ Parse the model name to extract the type suffix and route accordingly:
41
+
42
+ ### Type: `.mas` (Model Aggregation Service)
43
+ - **Tool**: `model-score`
44
+ - **Use for**: Standard MAS-deployed predictive models
45
+ - **Example**: `score with model churn.mas scenario =age=45,income=60000`
46
+ - **Invocation**: `model-score({ model: "churn", scenario: {...} })`
47
+
48
+ ### Type: `.job` (SAS Viya Job)
49
+ - **Tool**: `run-job`
50
+ - **Use for**: Pre-built scoring jobs with parameters
51
+ - **Example**: `score with model monthly_scorer.job scenario =month=10,year=2025`
52
+ - **Invocation**: `run-job({ name: "monthly_scorer", scenario: {...} })`
53
+
54
+ ### Type: `.jobdef` (SAS Viya Job Definition)
55
+ - **Tool**: `run-jobdef`
56
+ - **Use for**: Job definitions that perform scoring logic
57
+ - **Example**: `score with model fraud_detector.jobdef using amount=500,merchant=online`
58
+ - **Invocation**: `run-jobdef({ name: "fraud_detector", scenario: {...} })`
59
+
60
+ ### Type: `.scr` (Score Code Runtime)
61
+ - **Tool**: `scr-score`
62
+ - **Use for**: Models deployed in SCR containers (REST endpoints)
63
+ - **Example**: `score https://scr-host/models/loan.scr using age=45,credit=700`
64
+ - **Invocation**: `scr-score({ url: "https://scr-host/models/loan", scenario: {...} })`
65
+
66
+ ### Type: `.sas` (SAS Program / SQL)
67
+ - **Tool**: `run-sas-program`
68
+ - **Use for**: Custom SAS or SQL scoring code
69
+ - **Example**: `score my_scoring_code.sas using x=1,y=2`
70
+ - **Invocation**: `run-sas-program({ folder: "my_scoring_code", scenario: {...} })`
71
+
72
+ ---
73
+
74
+ ## Scenario Parsing
75
+
76
+ The scenario parameter (comma-separated key=value pairs) is parsed into an object:
77
+
78
+ ```
79
+ scenario =age=45,income=60000,region=South
80
+ ↓ parsed as:
81
+ { age: "45", income: "60000", region: "South" }
82
+ ```
83
+
84
+ Accepted formats:
85
+ - **String**: `age=45,income=60000`
86
+ - **Object**: `{ age: 45, income: 60000 }`
87
+ - **Array** (batch): `[ {age:45, income:60000}, {age:50, income:75000} ]`
88
+
89
+ ---
90
+
91
+ ## Step 1 — Check model familiarity before scoring
92
+
93
+ Score immediately if:
94
+ - The user names a specific model they've used before in this session, OR
95
+ - The model name matches a previously confirmed model in the conversation
96
+
97
+ Pause and suggest investigation if:
98
+ - The model name is new, vague, or misspelled-looking (e.g. "the churn one", "that cancer model")
99
+ - The user seems unsure of the required input variable names
100
+
101
+ **Suggested message:**
102
+ > "I don't recognize that model — want me to run `find-model` to confirm it exists,
103
+ > or `model-info` to check its required inputs first?"
104
+
105
+ ---
106
+
107
+ ## Step 2 — Prepare the scenario data
108
+
109
+ **For a single record** (one object):
110
+ ```javascript
111
+ scenario = { field1: value1, field2: value2, ... }
112
+ ```
113
+
114
+ **For batch scoring** (multiple records — the typical case):
115
+ ```javascript
116
+ scenario = [
117
+ { field1: val1, field2: val2, ... },
118
+ { field1: val3, field2: val4, ... },
119
+ ...
120
+ ]
121
+ ```
122
+
123
+ **Critical rules:**
124
+ - Loop or call model-score **once per row**.
125
+ - Field names in the scenario must match the model's expected input variable names **exactly**.
126
+ - If table column names differ from model input names, **flag this to the user** and ask for confirmation before scoring.
127
+ - Example: Table has `age_years`, but model expects `age` → ask user which column maps to which input.
128
+ - Do not add units, labels, or extra metadata — raw field values only.
129
+
130
+ ---
131
+
132
+ ## Step 3 — Invoke the appropriate scoring tool
133
+
134
+ Based on the type extracted from the model name, invoke the corresponding tool:
135
+
136
+ **For `.mas` (default):**
137
+ ```javascript
138
+ model-score({
139
+ model: "<modelname>",
140
+ scenario: scenario, // object or array
141
+ uflag: false // set true if you need field names prefixed with _
142
+ })
143
+ ```
144
+
145
+ **For `.job`:**
146
+ ```javascript
147
+ run-job({
148
+ name: "<jobname>",
149
+ scenario: scenario
150
+ })
151
+ ```
152
+
153
+ **For `.jobdef`:**
154
+ ```javascript
155
+ run-jobdef({
156
+ name: "<jobdefname>",
157
+ scenario: scenario
158
+ })
159
+ ```
160
+
161
+ **For `.scr`:**
162
+ ```javascript
163
+ scr-score({
164
+ url: "<scr_endpoint_url>",
165
+ scenario: scenario
166
+ })
167
+ ```
168
+
169
+ **For `.sas`:**
170
+ ```javascript
171
+ run-sas-program({
172
+ src: "<sas_or_sql_code>",
173
+ scenario: scenario
174
+ })
175
+ ```
176
+
177
+ **Rules:**
178
+ - Pass the full batch in one call; do not loop over rows
179
+ - If scoring fails, return the structured error and suggest troubleshooting
180
+ - For MAS models, include uflag parameter if underscore-prefixed output is needed
181
+ - For jobs/jobdefs, scenario becomes parameter arguments
182
+ - For SCR, include full URL endpoint
183
+
184
+ ---
185
+
186
+ ## Step 4 — Present the results
187
+
188
+ Merge the scoring output back with the input records and present as a table where possible.
189
+
190
+ **Always surface:**
191
+ - The key prediction/score field(s) (e.g. `P_churn`, `score`, `prediction`, `P_risk`)
192
+ - Any probability/confidence fields for classification models (e.g. `P_class0`, `P_class1`)
193
+ - Selected input fields that drove the prediction, so the user can see context
194
+
195
+ **Formatting:**
196
+ - Present results in a table for clarity
197
+ - If results exceed 10 rows, show the first 10 and ask: *"Want to see more results or export the full set?"*
198
+ - Round numeric predictions to 2–4 decimal places for readability
199
+
200
+ ---
201
+
202
+ ## Common flows
203
+
204
+ **Flow A — Score rows with MAS model**
205
+ > "Score the first 10 customers in Public.customers with the churn model"
206
+
207
+ 1. `read-table` → { table: "Public.customers", limit: 10 }
208
+ 2. `model-score` → { model: "churn", scenario: [ ...10 row objects ] }
209
+ 3. Present merged results with prediction + key inputs
210
+
211
+ **Flow B — Score with a scoring job**
212
+ > "Score December sales with the monthly_scorer job using month=12,year=2025"
213
+
214
+ 1. `run-job` → { name: "monthly_scorer", scenario: { month: "12", year: "2025" } }
215
+ 2. Capture job output and tables
216
+ 3. Present results
217
+
218
+ **Flow C — Score with a job definition**
219
+ > "Run fraud detection jobdef on transaction amount=500, merchant=online"
220
+
221
+ 1. `run-jobdef` → { name: "fraud_detection", scenario: { amount: "500", merchant: "online" } }
222
+ 2. Capture log, listings, and tables
223
+ 3. Present results
224
+
225
+ **Flow D — Score with SCR endpoint**
226
+ > "Score with the loan model at https://scr-host/models/loan using age=45, credit_score=700"
227
+
228
+ 1. `scr-score` → { url: "https://scr-host/models/loan", scenario: { age: "45", credit_score: "700" } }
229
+ 2. Capture prediction response
230
+ 3. Present result
231
+
232
+ **Flow E — Score results of an analytical query with MAS**
233
+ > "Score high-value customers (spend > 5000) in mylib.sales with the fraud model"
234
+
235
+ 1. `sas-query` → { table: "mylib.sales", sql: "SELECT * FROM mylib.sales WHERE spend > 5000" }
236
+ 2. `model-score` → { model: "fraud", scenario: [ ...result rows ] }
237
+ 3. Present merged results
238
+
239
+ **Flow F — User supplies scenario data directly**
240
+ > "Score age=45, income=60000, region=South with the churn model"
241
+
242
+ 1. Skip read step
243
+ 2. `model-score` → { model: "churn", scenario: { age: "45", income: "60000", region: "South" } }
244
+ 3. Present result
245
+
246
+ **Flow G — Model unfamiliar, need to confirm**
247
+ > "Score Public.applicants with the creditRisk2 model"
248
+
249
+ 1. Pause — "creditRisk2" is new
250
+ 2. Suggest: `find-model` to confirm it exists, `model-info` to get input variables
251
+ 3. Once confirmed → `read-table` + `model-score`
252
+
253
+ **Flow H — Generic score syntax with type routing**
254
+ > "score with model churn.mas scenario =age=45,income=60000"
255
+ > "score fraud_detector.jobdef where scenario =amount=500"
256
+ > "score monthly_report.job using month=10,year=2025"
257
+
258
+ 1. Parse model name to extract type (.mas, .job, .jobdef, .scr, .sas)
259
+ 2. Route to appropriate tool based on type
260
+ 3. Parse scenario and invoke tool with parameters
261
+ 4. Present results from routed tool
262
+
263
+ ---
264
+
265
+ ## Error handling
266
+
267
+ | Problem | Action |
268
+ |---|---|
269
+ | Model not found | Suggest `find-model` to verify the model is deployed |
270
+ | Input field name mismatch | Show the mismatch (table has X, model expects Y), ask user to confirm mapping |
271
+ | Scoring error / invalid inputs | Return structured error, suggest `model-info` to check required inputs and data types |
272
+ | Empty read result | Tell user, ask if they want to adjust the query/filter before scoring |
273
+ | Missing input fields | Ask which table columns map to the required model inputs |
274
+
275
+ ---
276
+
277
+ ## Tips
278
+
279
+ - **Batch is better:** Always pass the full set of records in one `model-score` call. Do not loop.
280
+ - **Confirm mappings:** If column names don't match model inputs, ask before scoring.
281
+ - **Show context:** Include key input columns in the result output so predictions make sense.
282
+ - **Limit output:** For large result sets (>10 rows), ask before showing all.
@@ -77,6 +77,7 @@ async function createMcpServer(cache, _appContext) {
77
77
  let toolHandler = wrapf(cache, tool.handler);
78
78
 
79
79
  mcpServer.tool(toolName, tool.description, tool.schema, toolHandler);
80
+
80
81
  toolNames.push(toolName);
81
82
  });
82
83
  console.error(`[Note] Registered ${toolSet.length} tools: ${toolNames}`);
@@ -6,17 +6,14 @@ import express from "express";
6
6
 
7
7
  import https from "https";
8
8
  import cors from "cors";
9
- //import rateLimit from "express-rate-limit";
10
- //import helmet from "helmet";
11
9
  import bodyParser from "body-parser";
12
-
13
10
  import selfsigned from "selfsigned";
14
11
  import openAPIJson from "./openAPIJson.js";
15
- import fs from "fs";
16
12
 
17
13
  import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
18
14
  import { randomUUID } from "node:crypto";
19
15
  import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
16
+ import tlogon from "./toolHelpers/tlogon.js";
20
17
 
21
18
 
22
19
  // setup express server
@@ -98,10 +95,9 @@ app.get("/openapi.json", (req, res) => {
98
95
 
99
96
  // handle processing of information in header.
100
97
  function requireBearer(req, res, next) {
101
-
102
-
103
98
  // process any new header information
104
-
99
+ console.error("=======================================================");
100
+ console.error("Processing headers for incoming request to /mcp endpoint");
105
101
  // Allow different VIYA server per sessionid(user)
106
102
  let headerCache = {};
107
103
  if (req.header("X-VIYA-SERVER") != null) {
@@ -115,29 +111,44 @@ function requireBearer(req, res, next) {
115
111
  if (hdr != null) {
116
112
  headerCache.bearerToken = hdr.slice(7);
117
113
  headerCache.AUTHFLOW = "bearer";
114
+ console.error("[Note] Using user supplied bearer token for authorization");
115
+ console.error("[Debug] Bearer token starts with:", headerCache.bearerToken);
116
+ } else {
117
+ console.error("[Note] No bearer token supplied in Authorization header");
118
+ headerCache.bearerToken = null;
118
119
  }
119
120
 
120
121
  // faking out api key since Viya does not support
121
122
  // not ideal for production
122
123
  const hdr2 = req.header("X-REFRESH-TOKEN");
123
124
  if (hdr2 != null) {
124
- headerCache.refreshToken = hdr2;
125
+ headerCache.REFRESH_TOKEN = hdr2;
125
126
  headerCache.AUTHFLOW = "refresh";
127
+ console.error("[Note] Using user supplied refresh token for authorization");
126
128
  }
127
129
  cache.set("headerCache", headerCache);
128
130
  next();
131
+ console.error("Finished processing headers for /mcp request");
132
+ console.log("=======================================================");
129
133
  }
130
134
 
131
135
  // process mcp endpoint requests
132
136
  const handleRequest = async (req, res) => {
133
- let transport;
137
+ let transport = null;
134
138
  let transports = cache.get("transports");
139
+ console.error("=========================================================");
140
+ console.error("Processing POST /mcp request");
141
+ if (transports == null) {
142
+ console.error("[Error] ***** transports cache is null. This is an error");
143
+ transports = {};
144
+ cache.set("transports", transports);
145
+ }
146
+
135
147
  console.error("current transports in cache:", Object.keys(transports));
136
148
  try {
137
149
 
138
150
  let sessionId = req.headers["mcp-session-id"];
139
- console.error("========================================================");
140
- console.error("post /mcp called with session ID:", sessionId);
151
+ console.error("[Note]Incoming session ID:", sessionId);
141
152
  let body = (req.body == null) ? 'no body' : JSON.stringify(req.body);
142
153
  console.error('[Note] Payload is ', body);
143
154
  if (/*!sessionId &&*/ isInitializeRequest(req.body)) {
@@ -157,7 +168,7 @@ const handleRequest = async (req, res) => {
157
168
  });
158
169
  // Clean up transport when closed
159
170
  transport.onclose = () => {
160
- if (transport.sessionId) {
171
+ if (transport.sessionId && transports[transport.sessionId]) {
161
172
  delete transports[transport.sessionId];
162
173
  }
163
174
  };
@@ -165,9 +176,11 @@ const handleRequest = async (req, res) => {
165
176
  await mcpServer.connect(transport);
166
177
 
167
178
  // Save transport data and app context for use in tools
168
- console.error('connected mcpServer');
179
+ console.error('[Note] Connected to mcpServer');
169
180
  cache.set("transports", transports);
181
+ console.error("=======================================================");
170
182
  return await transport.handleRequest(req, res, req.body);
183
+
171
184
  // cache transport
172
185
 
173
186
  } else if (sessionId != null) {
@@ -176,8 +189,8 @@ const handleRequest = async (req, res) => {
176
189
  console.error("[Note] Found transport:", transport != null);
177
190
  if (transport == null) {
178
191
  // this can happen if client is holding on to old session id
179
- console.error("[Error] No transport found for session ID:", sessionId, "Returning a 400 error with instructions for the user");
180
- res.status(400).send(`Invalid or missing session ID ${sessionId}. Please ensure your MCP client is configured to use the correct session ID returned in the 'mcp-session-id' header of the response from the /mcp endpoint.`);
192
+ console.error("[Error] No transport found for session ID:", sessionId, "Returning a 404 error with instructions for the user");
193
+ res.status(404).send(`Invalid or missing session ID ${sessionId}. Please ensure your MCP client is configured to use the correct session ID returned in the 'mcp-session-id' header of the response from the /mcp endpoint.`);
181
194
  return;
182
195
  }
183
196
 
@@ -194,9 +207,15 @@ const handleRequest = async (req, res) => {
194
207
  let headerCache = cache.get("headerCache");
195
208
  _appContext = Object.assign({}, appEnvTemplate, headerCache);
196
209
  cache.set(sessionId, _appContext);
210
+ } else {
211
+ let headerCache = cache.get("headerCache");
212
+ console.error('compare tokens', headerCache.bearerToken === _appContext.bearerToken);
213
+ _appContext = Object.assign(_appContext, headerCache);
214
+ console.error('New bearerToken:', _appContext.bearerToken);
215
+ cache.set(sessionId, _appContext);
197
216
  }
198
217
  console.error("[Note] Using existing transport for session ID:", sessionId);
199
-
218
+ console.error("==========================================================");
200
219
  await transport.handleRequest(req, res, req.body);
201
220
  return;
202
221
  }
@@ -220,20 +239,27 @@ const handleRequest = async (req, res) => {
220
239
  }
221
240
  };
222
241
  const handleGetDelete = async (req, res) => {
223
- console.error(req.method, "/mcp called");
242
+ console.error("=========================================================");
243
+ console.error(`[Note] ${req.method} /mcp called`);
224
244
  const sessionId = req.headers["mcp-session-id"];
225
- console.error("Handling GET/DELETE for session ID:", sessionId);
245
+ console.error("[Note] SessionId:", sessionId);
246
+
226
247
  let transports = cache.get("transports");
227
248
  let transport = (sessionId == null) ? null : transports[sessionId];
249
+ console.error("[Note] Transport found:", transport != null);
228
250
  if (!sessionId || transport == null) {
229
- res.status(400).send(`[Error] In ${req.method}: Invalid or missing session ID ${sessionId}`);
251
+ res.status(404).send(`[Error] In ${req.method}: Invalid or missing session ID ${sessionId}`);
230
252
  return;
231
253
  }
232
- await transport.handleRequest(req, res);
254
+ if (req.method === "GET") {
255
+ await transport.handleRequest(req, res);
256
+ return;
257
+ }
233
258
  if (req.method === "DELETE" && sessionId != null) {
234
- console.error("Deleting transport and cache for session ID:", sessionId);
259
+ console.error("[Note] Deleting transport and cache for session ID:", sessionId);
235
260
  delete transports[sessionId];
236
261
  cache.del(sessionId);
262
+ res.status(201).send(`[Info] Deleted session ${sessionId}`);
237
263
  }
238
264
  }
239
265
 
@@ -241,26 +267,40 @@ app.options("/mcp", (_, res) => res.sendStatus(204));
241
267
  app.post("/mcp", requireBearer, handleRequest);
242
268
  app.get("/mcp", handleGetDelete);
243
269
  app.delete("/mcp", handleGetDelete);
244
- app.get("/startup", (_req, res) => {
245
- console.error("Received request for startup endpoint");
270
+ app.get("/StartUp", (_req, res) => {
271
+ console.error("===================================================================")
272
+ console.error("Received request for Startup endpoint. Current app status:", appStatus);
273
+ console.error("===================================================================");
246
274
  if (appStatus === false) {
247
- return res.status(500).json({ status: "starting" });
275
+ return res.status(503).json({ status: "starting" });
248
276
  }
249
277
  return res.status(200).json({ status: "started" });
250
278
  });
251
-
279
+ app.get("/tlogon", async (_req, res) => {
280
+ console.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Testing logon");
281
+ if (appStatus === false) {
282
+ return res.status(503).json({ status: "not ready" });
283
+ }
284
+ let r = await tlogon(baseAppEnvContext);
285
+ console.error(r);
286
+ return res.status(200).json(r);
287
+ });
252
288
  app.get("/status", (_req, res) => {
289
+ console.error("===================================================================")
253
290
  console.error("Received request for status endpoint. Current app status:", appStatus);
291
+ console.error("===================================================================");
254
292
  if (appStatus === false) {
255
- return res.status(500).json({ status: "not ready" });
293
+ return res.status(503).json({ status: "not ready" });
256
294
  }
257
295
  return res.status(200).json({ status: "ready" });
258
296
  });
259
297
 
260
298
  app.get("/ready", (_req, res) => {
299
+ console.error("===================================================================")
261
300
  console.error("Received request for ready endpoint. Current app status:", appStatus);
301
+ console.error("===================================================================")
262
302
  if (appStatus === false) {
263
- return res.status(500).json({ status: "not ready" });
303
+ return res.status(503).json({ status: "not ready" });
264
304
  }
265
305
  return res.status(200).json({ status: "ready" });
266
306
  });
@@ -274,12 +314,12 @@ let appServer;
274
314
 
275
315
  // get TLS options
276
316
  if (appEnvBase.HTTPS === 'TRUE') {
277
- //appEnvBase.tlsOpts = getOpts(appEnvBase);
278
317
  if (appEnvBase.tlsOpts == null) {
279
318
  appEnvBase.tlsOpts = await getTls(appEnvBase);
280
319
  console.error(Object.keys(appEnvBase.tlsOpts));
281
320
  appEnvBase.tlsOpts.requestCert = false;
282
321
  appEnvBase.tlsOpts.rejectUnauthorized = false;
322
+ appEnvBase.contexts.appCert = appEnvBase.tlsOpts; /* just for completeness */
283
323
  }
284
324
 
285
325
  cache.set("appEnvBase", appEnvBase);
@@ -5,22 +5,25 @@
5
5
 
6
6
  async function handleGetDelete(mcpServer, cache, req, h) {
7
7
  const sessionId = req.headers["mcp-session-id"];
8
- console.error(`Handling ${req.method} for session ID:`, sessionId);
8
+ console.error("=======================================================");
9
+ console.error(`[Note] Handling ${req.method} for session ID:`, sessionId);
9
10
  let transports = cache.get("transports");
10
11
  let transport = transports[sessionId];
11
12
  if (!sessionId || transport == null) {
12
13
  console.error('[Note] Looks like a fresh start - no session id or transport found');
13
- h.abandon;
14
+ return h.abandon;
14
15
  }
15
16
 
16
17
  if (req.method === "GET") {
17
18
  // You can customize the response as needed
19
+ console.error("[Note] Payload:", req.payload);
20
+ console.log("======================================================");
18
21
  await transport.handleRequest(req.raw.req, req.raw.res, req.payload);
19
22
  return h.abandon;
20
23
  }
21
24
 
22
25
  if (req.method === "DELETE") {
23
- console.error("Deleting transport and cache for session ID:", sessionId);
26
+ console.error("[Note] Deleting transport and cache for session ID:", sessionId);
24
27
  delete transports[sessionId];
25
28
  cache.del(sessionId);
26
29
  return h.response(`[Info] In DELETE: Session ID ${sessionId} deleted`).code(201);