fixparser-plugin-mcp 9.1.7-a24bdbec → 9.1.7-abf6e45c

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.
@@ -1,179 +1,106 @@
1
1
  // src/MCPLocal.ts
2
2
  import { Server } from "@modelcontextprotocol/sdk/server/index.js";
3
3
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4
- import {
5
- CallToolRequestSchema,
6
- GetPromptRequestSchema,
7
- ListPromptsRequestSchema,
8
- ListResourcesRequestSchema,
9
- ListToolsRequestSchema
10
- } from "@modelcontextprotocol/sdk/types.js";
11
4
  import { Field, Fields, Messages } from "fixparser";
12
- var parseInputSchema = {
13
- type: "object",
14
- properties: {
15
- fixString: {
16
- type: "string",
17
- description: "FIX message string to parse"
18
- }
19
- },
20
- required: ["fixString"]
21
- };
22
- var parseToJSONInputSchema = {
23
- type: "object",
24
- properties: {
25
- fixString: {
26
- type: "string",
27
- description: "FIX message string to parse"
28
- }
29
- },
30
- required: ["fixString"]
31
- };
32
- var newOrderSingleInputSchema = {
33
- type: "object",
34
- properties: {
35
- clOrdID: {
36
- type: "string",
37
- description: "Client Order ID"
38
- },
39
- handlInst: {
40
- type: "string",
41
- enum: ["1", "2", "3"],
42
- description: 'Handling instruction (IMPORTANT: Use the numeric/alphabetic value, not the descriptive name. For example, use "1" for Manual, "2" for Automated, "3" for AutomatedNoIntervention)'
43
- },
44
- quantity: {
45
- type: "number",
46
- description: "Order quantity"
47
- },
48
- price: {
49
- type: "number",
50
- description: "Order price"
51
- },
52
- ordType: {
53
- type: "string",
54
- enum: [
55
- "1",
56
- "2",
57
- "3",
58
- "4",
59
- "5",
60
- "6",
61
- "7",
62
- "8",
63
- "9",
64
- "A",
65
- "B",
66
- "C",
67
- "D",
68
- "E",
69
- "F",
70
- "G",
71
- "H",
72
- "I",
73
- "J",
74
- "K",
75
- "L",
76
- "M",
77
- "P",
78
- "Q",
79
- "R",
80
- "S"
81
- ],
82
- description: 'Order type (IMPORTANT: Use the numeric/alphabetic value, not the descriptive name. For example, use "1" for Market, "2" for Limit, "3" for Stop)'
83
- },
84
- side: {
85
- type: "string",
86
- enum: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H"],
87
- description: 'Order side (IMPORTANT: Use the numeric/alphabetic value, not the descriptive name. For example, use "1" for Buy, "2" for Sell, "3" for BuyMinus, "4" for SellPlus, "5" for SellShort, "6" for SellShortExempt, "7" for Undisclosed, "8" for Cross, "9" for CrossShort, "A" for CrossShortExempt, "B" for AsDefined, "C" for Opposite, "D" for Subscribe, "E" for Redeem, "F" for Lend, "G" for Borrow, "H" for SellUndisclosed)'
88
- },
89
- symbol: {
90
- type: "string",
91
- description: "Trading symbol"
92
- },
93
- timeInForce: {
94
- type: "string",
95
- enum: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C"],
96
- description: 'Time in force (IMPORTANT: Use the numeric/alphabetic value, not the descriptive name. For example, use "0" for Day, "1" for Good Till Cancel, "2" for At Opening, "3" for Immediate or Cancel, "4" for Fill or Kill, "5" for Good Till Crossing, "6" for Good Till Date)'
97
- }
98
- },
99
- required: ["clOrdID", "quantity", "price", "side", "symbol", "handlInst", "ordType", "timeInForce"]
100
- };
101
- var marketDataRequestInputSchema = {
102
- type: "object",
103
- properties: {
104
- mdUpdateType: {
105
- type: "string",
106
- enum: ["0", "1"],
107
- description: 'Market data update type (IMPORTANT: Use the numeric/alphabetic value, not the descriptive name. For example, use "0" for FullRefresh, "1" for IncrementalRefresh)'
108
- },
109
- symbol: {
110
- type: "string",
111
- description: "Trading symbol"
112
- },
113
- mdReqID: {
114
- type: "string",
115
- description: "Market data request ID"
116
- },
117
- subscriptionRequestType: {
118
- type: "string",
119
- enum: ["0", "1", "2"],
120
- description: 'Subscription request type (IMPORTANT: Use the numeric/alphabetic value, not the descriptive name. For example, use "0" for Snapshot + Updates, "1" for Snapshot, "2" for Unsubscribe)'
121
- },
122
- mdEntryType: {
123
- type: "string",
124
- enum: [
125
- "0",
126
- "1",
127
- "2",
128
- "3",
129
- "4",
130
- "5",
131
- "6",
132
- "7",
133
- "8",
134
- "9",
135
- "A",
136
- "B",
137
- "C",
138
- "D",
139
- "E",
140
- "F",
141
- "G",
142
- "H",
143
- "J",
144
- "K",
145
- "L",
146
- "M",
147
- "N",
148
- "O",
149
- "P",
150
- "Q",
151
- "S",
152
- "R",
153
- "T",
154
- "U",
155
- "V",
156
- "W",
157
- "X",
158
- "Y",
159
- "Z",
160
- "a",
161
- "b",
162
- "c",
163
- "d",
164
- "e",
165
- "g",
166
- "h",
167
- "i",
168
- "t"
169
- ],
170
- description: 'Market data entry type (IMPORTANT: Use the numeric/alphabetic value, not the descriptive name. For example, use "0" for Bid, "1" for Offer, "2" for Trade, "3" for Index Value, "4" for Opening Price)'
171
- }
172
- },
173
- required: ["symbol", "mdReqID"]
174
- };
5
+ import { z } from "zod";
6
+ var symbolSchema = z.object({
7
+ symbol: z.string()
8
+ });
9
+ var fixStringSchema = z.object({
10
+ fixString: z.string()
11
+ });
12
+ var orderSchema = z.object({
13
+ clOrdID: z.string(),
14
+ handlInst: z.enum(["1", "2", "3"]),
15
+ quantity: z.string(),
16
+ price: z.string(),
17
+ ordType: z.enum([
18
+ "1",
19
+ "2",
20
+ "3",
21
+ "4",
22
+ "5",
23
+ "6",
24
+ "7",
25
+ "8",
26
+ "9",
27
+ "A",
28
+ "B",
29
+ "C",
30
+ "D",
31
+ "E",
32
+ "F",
33
+ "G",
34
+ "H",
35
+ "I",
36
+ "J",
37
+ "K",
38
+ "L",
39
+ "M",
40
+ "P",
41
+ "Q",
42
+ "R",
43
+ "S"
44
+ ]),
45
+ side: z.enum(["1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H"]),
46
+ symbol: z.string(),
47
+ timeInForce: z.enum(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C"])
48
+ });
49
+ var marketDataRequestSchema = z.object({
50
+ mdUpdateType: z.enum(["0", "1"]),
51
+ symbols: z.array(z.string()),
52
+ mdReqID: z.string(),
53
+ subscriptionRequestType: z.enum(["0", "1", "2"]),
54
+ mdEntryTypes: z.array(
55
+ z.enum([
56
+ "0",
57
+ "1",
58
+ "2",
59
+ "3",
60
+ "4",
61
+ "5",
62
+ "6",
63
+ "7",
64
+ "8",
65
+ "9",
66
+ "A",
67
+ "B",
68
+ "C",
69
+ "D",
70
+ "E",
71
+ "F",
72
+ "G",
73
+ "H",
74
+ "J",
75
+ "K",
76
+ "L",
77
+ "M",
78
+ "N",
79
+ "O",
80
+ "P",
81
+ "Q",
82
+ "R",
83
+ "S",
84
+ "T",
85
+ "U",
86
+ "V",
87
+ "W",
88
+ "X",
89
+ "Y",
90
+ "Z",
91
+ "a",
92
+ "b",
93
+ "c",
94
+ "d",
95
+ "e",
96
+ "g",
97
+ "h",
98
+ "i",
99
+ "t"
100
+ ])
101
+ )
102
+ });
175
103
  var MCPLocal = class {
176
- // private logger: Logger | undefined;
177
104
  parser;
178
105
  server = new Server(
179
106
  {
@@ -182,43 +109,241 @@ var MCPLocal = class {
182
109
  },
183
110
  {
184
111
  capabilities: {
185
- tools: {},
186
- prompts: {},
187
- resources: {}
112
+ tools: {
113
+ parse: {
114
+ description: "Parses a FIX message and describes it in plain language",
115
+ parameters: {
116
+ type: "object",
117
+ properties: {
118
+ fixString: { type: "string" }
119
+ },
120
+ required: ["fixString"]
121
+ }
122
+ },
123
+ parseToJSON: {
124
+ description: "Parses a FIX message into JSON",
125
+ parameters: {
126
+ type: "object",
127
+ properties: {
128
+ fixString: { type: "string" }
129
+ },
130
+ required: ["fixString"]
131
+ }
132
+ },
133
+ verifyOrder: {
134
+ description: "Verifies order parameters before execution",
135
+ parameters: {
136
+ type: "object",
137
+ properties: {
138
+ clOrdID: { type: "string" },
139
+ handlInst: { type: "string", enum: ["1", "2", "3"] },
140
+ quantity: { type: "string" },
141
+ price: { type: "string" },
142
+ ordType: {
143
+ type: "string",
144
+ enum: [
145
+ "1",
146
+ "2",
147
+ "3",
148
+ "4",
149
+ "5",
150
+ "6",
151
+ "7",
152
+ "8",
153
+ "9",
154
+ "A",
155
+ "B",
156
+ "C",
157
+ "D",
158
+ "E",
159
+ "F",
160
+ "G",
161
+ "H",
162
+ "I",
163
+ "J",
164
+ "K",
165
+ "L",
166
+ "M",
167
+ "P",
168
+ "Q",
169
+ "R",
170
+ "S"
171
+ ]
172
+ },
173
+ side: {
174
+ type: "string",
175
+ enum: [
176
+ "1",
177
+ "2",
178
+ "3",
179
+ "4",
180
+ "5",
181
+ "6",
182
+ "7",
183
+ "8",
184
+ "9",
185
+ "A",
186
+ "B",
187
+ "C",
188
+ "D",
189
+ "E",
190
+ "F",
191
+ "G",
192
+ "H"
193
+ ]
194
+ },
195
+ symbol: { type: "string" },
196
+ timeInForce: {
197
+ type: "string",
198
+ enum: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C"]
199
+ }
200
+ },
201
+ required: [
202
+ "clOrdID",
203
+ "handlInst",
204
+ "quantity",
205
+ "price",
206
+ "ordType",
207
+ "side",
208
+ "symbol",
209
+ "timeInForce"
210
+ ]
211
+ }
212
+ },
213
+ executeOrder: {
214
+ description: "Executes a verified order",
215
+ parameters: {
216
+ type: "object",
217
+ properties: {
218
+ clOrdID: { type: "string" },
219
+ handlInst: { type: "string", enum: ["1", "2", "3"] },
220
+ quantity: { type: "string" },
221
+ price: { type: "string" },
222
+ ordType: { type: "string" },
223
+ side: { type: "string" },
224
+ symbol: { type: "string" },
225
+ timeInForce: { type: "string" }
226
+ },
227
+ required: [
228
+ "clOrdID",
229
+ "handlInst",
230
+ "quantity",
231
+ "price",
232
+ "ordType",
233
+ "side",
234
+ "symbol",
235
+ "timeInForce"
236
+ ]
237
+ }
238
+ },
239
+ marketDataRequest: {
240
+ description: "Requests market data for specified symbols",
241
+ parameters: {
242
+ type: "object",
243
+ properties: {
244
+ mdUpdateType: { type: "string", enum: ["0", "1"] },
245
+ symbols: { type: "array", items: { type: "string" } },
246
+ mdReqID: { type: "string" },
247
+ subscriptionRequestType: { type: "string", enum: ["0", "1", "2"] },
248
+ mdEntryTypes: { type: "array", items: { type: "string" } }
249
+ },
250
+ required: ["mdUpdateType", "symbols", "mdReqID", "subscriptionRequestType", "mdEntryTypes"]
251
+ }
252
+ }
253
+ },
254
+ resources: {
255
+ greeting: {
256
+ description: "A simple greeting resource",
257
+ uri: "greeting-resource"
258
+ },
259
+ stockGraph: {
260
+ description: "Generates a price chart for a given symbol",
261
+ uri: "stockGraph",
262
+ parameters: {
263
+ type: "object",
264
+ properties: {
265
+ symbol: { type: "string" }
266
+ },
267
+ required: ["symbol"]
268
+ }
269
+ },
270
+ stockPriceHistory: {
271
+ description: "Returns price history for a given symbol",
272
+ uri: "stockPriceHistory",
273
+ parameters: {
274
+ type: "object",
275
+ properties: {
276
+ symbol: { type: "string" }
277
+ },
278
+ required: ["symbol"]
279
+ }
280
+ }
281
+ }
188
282
  }
189
283
  }
190
284
  );
191
285
  transport = new StdioServerTransport();
192
286
  onReady = void 0;
193
287
  pendingRequests = /* @__PURE__ */ new Map();
288
+ verifiedOrders = /* @__PURE__ */ new Map();
289
+ // Store market data prices with timestamps
290
+ marketDataPrices = /* @__PURE__ */ new Map();
291
+ MAX_PRICE_HISTORY = 1e5;
292
+ // Maximum number of price points to store per symbol
194
293
  constructor({ logger, onReady }) {
195
294
  if (onReady) this.onReady = onReady;
196
295
  }
197
296
  async register(parser) {
198
297
  this.parser = parser;
199
298
  this.parser.addOnMessageCallback((message) => {
299
+ this.parser?.logger.log({
300
+ level: "info",
301
+ message: `MCP Server received message: ${message.messageType}: ${message.description}`
302
+ });
200
303
  const msgType = message.messageType;
201
- if (msgType === Messages.MarketDataSnapshotFullRefresh || msgType === Messages.ExecutionReport || msgType === Messages.Reject) {
202
- const idField = msgType === Messages.MarketDataSnapshotFullRefresh ? message.getField(Fields.MDReqID) : msgType === Messages.Reject ? message.getField(Fields.RefSeqNum) : message.getField(Fields.ClOrdID);
203
- if (idField) {
204
- const id = idField.value;
205
- if (typeof id === "string" || typeof id === "number") {
206
- if (msgType === Messages.Reject) {
207
- const refMsgType = message.getField(Fields.RefMsgType);
208
- if (refMsgType && refMsgType.value === Messages.NewOrderSingle) {
209
- const callback = this.pendingRequests.get(String(id));
210
- if (callback) {
211
- callback(message);
212
- this.pendingRequests.delete(String(id));
213
- }
214
- }
215
- } else {
216
- const callback = this.pendingRequests.get(String(id));
217
- if (callback) {
218
- callback(message);
219
- this.pendingRequests.delete(String(id));
220
- }
304
+ if (msgType === Messages.MarketDataSnapshotFullRefresh || msgType === Messages.ExecutionReport || msgType === Messages.Reject || msgType === Messages.MarketDataIncrementalRefresh) {
305
+ this.parser?.logger.log({
306
+ level: "info",
307
+ message: `MCP Server handling message type: ${msgType}`
308
+ });
309
+ let id;
310
+ if (msgType === Messages.MarketDataIncrementalRefresh || msgType === Messages.MarketDataSnapshotFullRefresh) {
311
+ const symbol = message.getField(Fields.Symbol);
312
+ const price = message.getField(Fields.MDEntryPx);
313
+ const timestamp = message.getField(Fields.MDEntryTime)?.value || Date.now();
314
+ if (symbol?.value && price?.value) {
315
+ const symbolStr = String(symbol.value);
316
+ const priceNum = Number(price.value);
317
+ const priceHistory = this.marketDataPrices.get(symbolStr) || [];
318
+ priceHistory.push({
319
+ timestamp: Number(timestamp),
320
+ price: priceNum
321
+ });
322
+ if (priceHistory.length > this.MAX_PRICE_HISTORY) {
323
+ priceHistory.shift();
221
324
  }
325
+ this.marketDataPrices.set(symbolStr, priceHistory);
326
+ this.parser?.logger.log({
327
+ level: "info",
328
+ message: `MCP Server added ${symbol}: ${priceNum}`
329
+ });
330
+ }
331
+ }
332
+ if (msgType === Messages.MarketDataSnapshotFullRefresh) {
333
+ const mdReqID = message.getField(Fields.MDReqID);
334
+ if (mdReqID) id = String(mdReqID.value);
335
+ } else if (msgType === Messages.ExecutionReport) {
336
+ const clOrdID = message.getField(Fields.ClOrdID);
337
+ if (clOrdID) id = String(clOrdID.value);
338
+ } else if (msgType === Messages.Reject) {
339
+ const refSeqNum = message.getField(Fields.RefSeqNum);
340
+ if (refSeqNum) id = String(refSeqNum.value);
341
+ }
342
+ if (id) {
343
+ const callback = this.pendingRequests.get(id);
344
+ if (callback) {
345
+ callback(message);
346
+ this.pendingRequests.delete(id);
222
347
  }
223
348
  }
224
349
  }
@@ -236,433 +361,732 @@ var MCPLocal = class {
236
361
  if (!this.server) {
237
362
  return;
238
363
  }
239
- const validateArgs = (args, schema) => {
240
- const result = {};
241
- for (const [key, propSchema] of Object.entries(schema.properties || {})) {
242
- const prop = propSchema;
243
- const value = args?.[key];
244
- if (prop.required && (value === void 0 || value === null)) {
245
- throw new Error(`Required property '${key}' is missing`);
246
- }
247
- if (value !== void 0) {
248
- result[key] = value;
249
- } else if (prop.default !== void 0) {
250
- result[key] = prop.default;
251
- }
364
+ this.server.setRequestHandler(
365
+ z.object({ method: z.literal("resources/list") }),
366
+ async (request, extra) => {
367
+ return {
368
+ resources: [
369
+ {
370
+ name: "greeting",
371
+ description: "A simple greeting resource",
372
+ uri: "greeting-resource"
373
+ },
374
+ {
375
+ name: "stockGraph",
376
+ description: "Generates a price chart for a given symbol",
377
+ uri: "stockGraph",
378
+ parameters: {
379
+ type: "object",
380
+ properties: {
381
+ symbol: { type: "string" }
382
+ },
383
+ required: ["symbol"]
384
+ }
385
+ },
386
+ {
387
+ name: "stockPriceHistory",
388
+ description: "Returns price history for a given symbol",
389
+ uri: "stockPriceHistory",
390
+ parameters: {
391
+ type: "object",
392
+ properties: {
393
+ symbol: { type: "string" }
394
+ },
395
+ required: ["symbol"]
396
+ }
397
+ }
398
+ ]
399
+ };
252
400
  }
253
- return result;
254
- };
255
- this.server.setRequestHandler(ListResourcesRequestSchema, async () => {
256
- return {
257
- resources: []
258
- };
259
- });
260
- this.server.setRequestHandler(ListToolsRequestSchema, async () => {
261
- return {
262
- tools: [
263
- {
264
- name: "parse",
265
- description: "Parses a FIX message and describes it in plain language",
266
- inputSchema: parseInputSchema
267
- },
268
- {
269
- name: "parseToJSON",
270
- description: "Parses a FIX message into JSON",
271
- inputSchema: parseToJSONInputSchema
272
- },
273
- {
274
- name: "newOrderSingle",
275
- description: "Creates and sends a New Order Single",
276
- inputSchema: newOrderSingleInputSchema
277
- },
278
- {
279
- name: "marketDataRequest",
280
- description: "Sends a request for Market Data with the given symbol",
281
- inputSchema: marketDataRequestInputSchema
282
- }
283
- ]
284
- };
285
- });
286
- this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
287
- const { name, arguments: args } = request.params;
288
- switch (name) {
289
- case "parse": {
290
- try {
291
- const { fixString } = validateArgs(args, parseInputSchema);
292
- const parsedMessage = this.parser?.parse(fixString);
293
- if (!parsedMessage || parsedMessage.length === 0) {
294
- return {
295
- isError: true,
296
- content: [{ type: "text", text: "Error: Failed to parse FIX string" }]
297
- };
401
+ );
402
+ this.server.setRequestHandler(
403
+ z.object({ method: z.literal("tools/list") }),
404
+ async (request, extra) => {
405
+ return {
406
+ tools: [
407
+ {
408
+ name: "parse",
409
+ description: "Parses a FIX message and describes it in plain language",
410
+ parameters: {
411
+ type: "object",
412
+ properties: {
413
+ fixString: { type: "string" }
414
+ },
415
+ required: ["fixString"]
416
+ }
417
+ },
418
+ {
419
+ name: "parseToJSON",
420
+ description: "Parses a FIX message into JSON",
421
+ parameters: {
422
+ type: "object",
423
+ properties: {
424
+ fixString: { type: "string" }
425
+ },
426
+ required: ["fixString"]
427
+ }
428
+ },
429
+ {
430
+ name: "verifyOrder",
431
+ description: "Verifies order parameters before execution",
432
+ parameters: {
433
+ type: "object",
434
+ properties: {
435
+ clOrdID: { type: "string" },
436
+ handlInst: { type: "string", enum: ["1", "2", "3"] },
437
+ quantity: { type: "string" },
438
+ price: { type: "string" },
439
+ ordType: {
440
+ type: "string",
441
+ enum: [
442
+ "1",
443
+ "2",
444
+ "3",
445
+ "4",
446
+ "5",
447
+ "6",
448
+ "7",
449
+ "8",
450
+ "9",
451
+ "A",
452
+ "B",
453
+ "C",
454
+ "D",
455
+ "E",
456
+ "F",
457
+ "G",
458
+ "H",
459
+ "I",
460
+ "J",
461
+ "K",
462
+ "L",
463
+ "M",
464
+ "P",
465
+ "Q",
466
+ "R",
467
+ "S"
468
+ ]
469
+ },
470
+ side: {
471
+ type: "string",
472
+ enum: [
473
+ "1",
474
+ "2",
475
+ "3",
476
+ "4",
477
+ "5",
478
+ "6",
479
+ "7",
480
+ "8",
481
+ "9",
482
+ "A",
483
+ "B",
484
+ "C",
485
+ "D",
486
+ "E",
487
+ "F",
488
+ "G",
489
+ "H"
490
+ ]
491
+ },
492
+ symbol: { type: "string" },
493
+ timeInForce: {
494
+ type: "string",
495
+ enum: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C"]
496
+ }
497
+ },
498
+ required: [
499
+ "clOrdID",
500
+ "handlInst",
501
+ "quantity",
502
+ "price",
503
+ "ordType",
504
+ "side",
505
+ "symbol",
506
+ "timeInForce"
507
+ ]
508
+ }
509
+ },
510
+ {
511
+ name: "executeOrder",
512
+ description: "Executes a verified order",
513
+ parameters: {
514
+ type: "object",
515
+ properties: {
516
+ clOrdID: { type: "string" },
517
+ handlInst: { type: "string", enum: ["1", "2", "3"] },
518
+ quantity: { type: "string" },
519
+ price: { type: "string" },
520
+ ordType: { type: "string" },
521
+ side: { type: "string" },
522
+ symbol: { type: "string" },
523
+ timeInForce: { type: "string" }
524
+ },
525
+ required: [
526
+ "clOrdID",
527
+ "handlInst",
528
+ "quantity",
529
+ "price",
530
+ "ordType",
531
+ "side",
532
+ "symbol",
533
+ "timeInForce"
534
+ ]
535
+ }
536
+ },
537
+ {
538
+ name: "marketDataRequest",
539
+ description: "Requests market data for specified symbols",
540
+ parameters: {
541
+ type: "object",
542
+ properties: {
543
+ mdUpdateType: { type: "string", enum: ["0", "1"] },
544
+ symbols: { type: "array", items: { type: "string" } },
545
+ mdReqID: { type: "string" },
546
+ subscriptionRequestType: { type: "string", enum: ["0", "1", "2"] },
547
+ mdEntryTypes: { type: "array", items: { type: "string" } }
548
+ },
549
+ required: [
550
+ "mdUpdateType",
551
+ "symbols",
552
+ "mdReqID",
553
+ "subscriptionRequestType",
554
+ "mdEntryTypes"
555
+ ]
556
+ }
298
557
  }
558
+ ]
559
+ };
560
+ }
561
+ );
562
+ this.server.setRequestHandler(
563
+ z.object({
564
+ method: z.literal("resources/read"),
565
+ params: z.object({
566
+ uri: z.string()
567
+ })
568
+ }),
569
+ async (request, extra) => {
570
+ const { uri } = request.params;
571
+ switch (uri) {
572
+ case "greeting-resource":
299
573
  return {
300
574
  content: [
301
575
  {
302
576
  type: "text",
303
- text: `${parsedMessage[0].description}
304
- ${parsedMessage[0].messageTypeDescription}`
577
+ text: "Hello, world!"
305
578
  }
306
579
  ]
307
580
  };
308
- } catch (error) {
581
+ case "stockGraph":
309
582
  return {
310
- isError: true,
311
583
  content: [
312
584
  {
313
585
  type: "text",
314
- text: `Error: ${error instanceof Error ? error.message : "Failed to parse FIX string"}`
586
+ text: "This resource requires a symbol parameter. Please use the stockGraph resource with a symbol parameter."
315
587
  }
316
588
  ]
317
589
  };
318
- }
319
- }
320
- case "parseToJSON": {
321
- try {
322
- const { fixString } = validateArgs(args, parseToJSONInputSchema);
323
- const parsedMessage = this.parser?.parse(fixString);
324
- if (!parsedMessage || parsedMessage.length === 0) {
325
- return {
326
- isError: true,
327
- content: [{ type: "text", text: "Error: Failed to parse FIX string" }]
328
- };
329
- }
590
+ case "stockPriceHistory":
330
591
  return {
331
592
  content: [
332
593
  {
333
594
  type: "text",
334
- text: `${parsedMessage[0].toFIXJSON()}`
595
+ text: "This resource requires a symbol parameter. Please use the stockPriceHistory resource with a symbol parameter."
335
596
  }
336
597
  ]
337
598
  };
338
- } catch (error) {
599
+ default:
339
600
  return {
340
- isError: true,
341
601
  content: [
342
602
  {
343
603
  type: "text",
344
- text: `Error: ${error instanceof Error ? error.message : "Failed to parse FIX string"}`
604
+ text: `Resource not found: ${uri}`
345
605
  }
346
- ]
606
+ ],
607
+ isError: true
608
+ };
609
+ }
610
+ }
611
+ );
612
+ this.server.setRequestHandler(
613
+ z.object({
614
+ method: z.literal("parse"),
615
+ params: fixStringSchema
616
+ }),
617
+ async (request, extra) => {
618
+ try {
619
+ const args = request.params;
620
+ const parsedMessage = this.parser?.parse(args.fixString);
621
+ if (!parsedMessage || parsedMessage.length === 0) {
622
+ return {
623
+ content: [{ type: "text", text: "Error: Failed to parse FIX string" }],
624
+ isError: true
347
625
  };
348
626
  }
627
+ return {
628
+ content: [
629
+ {
630
+ type: "text",
631
+ text: `${parsedMessage[0].description}
632
+ ${parsedMessage[0].messageTypeDescription}`
633
+ }
634
+ ]
635
+ };
636
+ } catch (error) {
637
+ return {
638
+ content: [
639
+ {
640
+ type: "text",
641
+ text: `Error: ${error instanceof Error ? error.message : "Failed to parse FIX string"}`
642
+ }
643
+ ],
644
+ isError: true
645
+ };
349
646
  }
350
- case "newOrderSingle": {
351
- try {
352
- const { clOrdID, handlInst, quantity, price, ordType, side, symbol, timeInForce } = validateArgs(args, newOrderSingleInputSchema);
353
- const response = new Promise((resolve) => {
354
- this.pendingRequests.set(clOrdID, resolve);
355
- });
356
- const order = this.parser?.createMessage(
357
- new Field(Fields.MsgType, Messages.NewOrderSingle),
358
- new Field(Fields.MsgSeqNum, this.parser?.getNextTargetMsgSeqNum()),
359
- new Field(Fields.SenderCompID, this.parser?.sender),
360
- new Field(Fields.TargetCompID, this.parser?.target),
361
- new Field(Fields.SendingTime, this.parser?.getTimestamp()),
362
- new Field(Fields.ClOrdID, clOrdID),
363
- new Field(Fields.Side, side),
364
- new Field(Fields.Symbol, symbol),
365
- new Field(Fields.OrderQty, quantity),
366
- new Field(Fields.Price, price),
367
- new Field(Fields.OrdType, ordType),
368
- new Field(Fields.HandlInst, handlInst),
369
- new Field(Fields.TimeInForce, timeInForce),
370
- new Field(Fields.TransactTime, this.parser?.getTimestamp())
371
- );
372
- if (!this.parser?.connected) {
373
- return {
374
- isError: true,
375
- content: [
376
- {
377
- type: "text",
378
- text: "Error: Not connected. Ignoring message."
379
- }
380
- ]
381
- };
382
- }
383
- this.parser?.send(order);
384
- const fixData = await response;
647
+ }
648
+ );
649
+ this.server.setRequestHandler(
650
+ z.object({
651
+ method: z.literal("parseToJSON"),
652
+ params: fixStringSchema
653
+ }),
654
+ async (request, extra) => {
655
+ try {
656
+ const args = request.params;
657
+ const parsedMessage = this.parser?.parse(args.fixString);
658
+ if (!parsedMessage || parsedMessage.length === 0) {
385
659
  return {
386
- content: [
387
- {
388
- type: "text",
389
- text: fixData.messageType === Messages.Reject ? `Reject message for order ${clOrdID}: ${JSON.stringify(fixData.toFIXJSON())}` : `Execution Report for order ${clOrdID}: ${JSON.stringify(fixData.toFIXJSON())}`
390
- }
391
- ]
660
+ content: [{ type: "text", text: "Error: Failed to parse FIX string" }],
661
+ isError: true
392
662
  };
393
- } catch (error) {
663
+ }
664
+ return {
665
+ content: [
666
+ {
667
+ type: "text",
668
+ text: `${parsedMessage[0].toFIXJSON()}`
669
+ }
670
+ ]
671
+ };
672
+ } catch (error) {
673
+ return {
674
+ content: [
675
+ {
676
+ type: "text",
677
+ text: `Error: ${error instanceof Error ? error.message : "Failed to parse FIX string"}`
678
+ }
679
+ ],
680
+ isError: true
681
+ };
682
+ }
683
+ }
684
+ );
685
+ this.server.setRequestHandler(
686
+ z.object({
687
+ method: z.literal("verifyOrder"),
688
+ params: orderSchema
689
+ }),
690
+ async (request, extra) => {
691
+ try {
692
+ const args = request.params;
693
+ this.verifiedOrders.set(args.clOrdID, {
694
+ clOrdID: args.clOrdID,
695
+ handlInst: args.handlInst,
696
+ quantity: Number.parseFloat(args.quantity),
697
+ price: Number.parseFloat(args.price),
698
+ ordType: args.ordType,
699
+ side: args.side,
700
+ symbol: args.symbol,
701
+ timeInForce: args.timeInForce
702
+ });
703
+ const ordTypeNames = {
704
+ "1": "Market",
705
+ "2": "Limit",
706
+ "3": "Stop",
707
+ "4": "StopLimit",
708
+ "5": "MarketOnClose",
709
+ "6": "WithOrWithout",
710
+ "7": "LimitOrBetter",
711
+ "8": "LimitWithOrWithout",
712
+ "9": "OnBasis",
713
+ A: "OnClose",
714
+ B: "LimitOnClose",
715
+ C: "ForexMarket",
716
+ D: "PreviouslyQuoted",
717
+ E: "PreviouslyIndicated",
718
+ F: "ForexLimit",
719
+ G: "ForexSwap",
720
+ H: "ForexPreviouslyQuoted",
721
+ I: "Funari",
722
+ J: "MarketIfTouched",
723
+ K: "MarketWithLeftOverAsLimit",
724
+ L: "PreviousFundValuationPoint",
725
+ M: "NextFundValuationPoint",
726
+ P: "Pegged",
727
+ Q: "CounterOrderSelection",
728
+ R: "StopOnBidOrOffer",
729
+ S: "StopLimitOnBidOrOffer"
730
+ };
731
+ const sideNames = {
732
+ "1": "Buy",
733
+ "2": "Sell",
734
+ "3": "BuyMinus",
735
+ "4": "SellPlus",
736
+ "5": "SellShort",
737
+ "6": "SellShortExempt",
738
+ "7": "Undisclosed",
739
+ "8": "Cross",
740
+ "9": "CrossShort",
741
+ A: "CrossShortExempt",
742
+ B: "AsDefined",
743
+ C: "Opposite",
744
+ D: "Subscribe",
745
+ E: "Redeem",
746
+ F: "Lend",
747
+ G: "Borrow",
748
+ H: "SellUndisclosed"
749
+ };
750
+ const timeInForceNames = {
751
+ "0": "Day",
752
+ "1": "GoodTillCancel",
753
+ "2": "AtTheOpening",
754
+ "3": "ImmediateOrCancel",
755
+ "4": "FillOrKill",
756
+ "5": "GoodTillCrossing",
757
+ "6": "GoodTillDate",
758
+ "7": "AtTheClose",
759
+ "8": "GoodThroughCrossing",
760
+ "9": "AtCrossing",
761
+ A: "GoodForTime",
762
+ B: "GoodForAuction",
763
+ C: "GoodForMonth"
764
+ };
765
+ const handlInstNames = {
766
+ "1": "AutomatedExecutionNoIntervention",
767
+ "2": "AutomatedExecutionInterventionOK",
768
+ "3": "ManualOrder"
769
+ };
770
+ return {
771
+ content: [
772
+ {
773
+ type: "text",
774
+ text: `VERIFICATION: All parameters valid. Ready to proceed with order execution.
775
+
776
+ Parameters verified:
777
+ - ClOrdID: ${args.clOrdID}
778
+ - HandlInst: ${args.handlInst} (${handlInstNames[args.handlInst]})
779
+ - Quantity: ${args.quantity}
780
+ - Price: ${args.price}
781
+ - OrdType: ${args.ordType} (${ordTypeNames[args.ordType]})
782
+ - Side: ${args.side} (${sideNames[args.side]})
783
+ - Symbol: ${args.symbol}
784
+ - TimeInForce: ${args.timeInForce} (${timeInForceNames[args.timeInForce]})
785
+
786
+ To execute this order, call the executeOrder tool with these exact same parameters.`
787
+ }
788
+ ]
789
+ };
790
+ } catch (error) {
791
+ return {
792
+ content: [
793
+ {
794
+ type: "text",
795
+ text: `Error: ${error instanceof Error ? error.message : "Failed to verify order parameters"}`
796
+ }
797
+ ],
798
+ isError: true
799
+ };
800
+ }
801
+ }
802
+ );
803
+ this.server.setRequestHandler(
804
+ z.object({
805
+ method: z.literal("executeOrder"),
806
+ params: orderSchema
807
+ }),
808
+ async (request, extra) => {
809
+ try {
810
+ const args = request.params;
811
+ const verifiedOrder = this.verifiedOrders.get(args.clOrdID);
812
+ if (!verifiedOrder) {
394
813
  return {
395
- isError: true,
396
814
  content: [
397
815
  {
398
816
  type: "text",
399
- text: `Error: ${error instanceof Error ? error.message : "Failed to create order"}`
817
+ text: `Error: Order ${args.clOrdID} has not been verified. Please call verifyOrder first.`
400
818
  }
401
- ]
819
+ ],
820
+ isError: true
402
821
  };
403
822
  }
404
- }
405
- case "marketDataRequest": {
406
- try {
407
- const { mdUpdateType, symbol, mdReqID, subscriptionRequestType, mdEntryType } = validateArgs(
408
- args,
409
- marketDataRequestInputSchema
410
- );
411
- const response = new Promise((resolve) => {
412
- this.pendingRequests.set(mdReqID, resolve);
413
- });
414
- const marketDataRequest = this.parser?.createMessage(
415
- new Field(Fields.MsgType, Messages.MarketDataRequest),
416
- new Field(Fields.SenderCompID, this.parser?.sender),
417
- new Field(Fields.MsgSeqNum, this.parser?.getNextTargetMsgSeqNum()),
418
- new Field(Fields.TargetCompID, this.parser?.target),
419
- new Field(Fields.SendingTime, this.parser?.getTimestamp()),
420
- new Field(Fields.MarketDepth, 0),
421
- new Field(Fields.MDUpdateType, mdUpdateType),
422
- new Field(Fields.NoRelatedSym, 1),
423
- new Field(Fields.Symbol, symbol),
424
- new Field(Fields.MDReqID, mdReqID),
425
- new Field(Fields.SubscriptionRequestType, subscriptionRequestType),
426
- new Field(Fields.NoMDEntryTypes, 1),
427
- new Field(Fields.MDEntryType, mdEntryType)
428
- );
429
- if (!this.parser?.connected) {
430
- return {
431
- isError: true,
432
- content: [
433
- {
434
- type: "text",
435
- text: "Error: Not connected. Ignoring message."
436
- }
437
- ]
438
- };
439
- }
440
- this.parser?.send(marketDataRequest);
441
- const fixData = await response;
823
+ if (verifiedOrder.handlInst !== args.handlInst || verifiedOrder.quantity !== Number.parseFloat(args.quantity) || verifiedOrder.price !== Number.parseFloat(args.price) || verifiedOrder.ordType !== args.ordType || verifiedOrder.side !== args.side || verifiedOrder.symbol !== args.symbol || verifiedOrder.timeInForce !== args.timeInForce) {
442
824
  return {
443
825
  content: [
444
826
  {
445
827
  type: "text",
446
- text: `Market data for ${symbol}: ${JSON.stringify(fixData.toFIXJSON())}`
828
+ text: "Error: Order parameters do not match the verified order. Please use the exact same parameters that were verified."
447
829
  }
448
- ]
830
+ ],
831
+ isError: true
449
832
  };
450
- } catch (error) {
833
+ }
834
+ const response = new Promise((resolve) => {
835
+ this.pendingRequests.set(args.clOrdID, resolve);
836
+ });
837
+ const order = this.parser?.createMessage(
838
+ new Field(Fields.MsgType, Messages.NewOrderSingle),
839
+ new Field(Fields.MsgSeqNum, this.parser?.getNextTargetMsgSeqNum()),
840
+ new Field(Fields.SenderCompID, this.parser?.sender),
841
+ new Field(Fields.TargetCompID, this.parser?.target),
842
+ new Field(Fields.SendingTime, this.parser?.getTimestamp()),
843
+ new Field(Fields.ClOrdID, args.clOrdID),
844
+ new Field(Fields.Side, args.side),
845
+ new Field(Fields.Symbol, args.symbol),
846
+ new Field(Fields.OrderQty, Number.parseFloat(args.quantity)),
847
+ new Field(Fields.Price, Number.parseFloat(args.price)),
848
+ new Field(Fields.OrdType, args.ordType),
849
+ new Field(Fields.HandlInst, args.handlInst),
850
+ new Field(Fields.TimeInForce, args.timeInForce),
851
+ new Field(Fields.TransactTime, this.parser?.getTimestamp())
852
+ );
853
+ if (!this.parser?.connected) {
451
854
  return {
452
- isError: true,
453
855
  content: [
454
856
  {
455
857
  type: "text",
456
- text: `Error: ${error instanceof Error ? error.message : "Failed to request market data"}`
858
+ text: "Error: Not connected. Ignoring message."
457
859
  }
458
- ]
860
+ ],
861
+ isError: true
459
862
  };
460
863
  }
461
- }
462
- default:
463
- throw new Error(`Unknown tool: ${name}`);
464
- }
465
- });
466
- this.server.setRequestHandler(ListPromptsRequestSchema, async () => {
467
- return {
468
- prompts: [
469
- {
470
- name: "parse",
471
- description: "Parses a FIX message and describes it in plain language",
472
- arguments: [
473
- {
474
- name: "fixString",
475
- description: "FIX message string to parse",
476
- required: true
477
- }
478
- ]
479
- },
480
- {
481
- name: "parseToJSON",
482
- description: "Parses a FIX message into JSON",
483
- arguments: [
484
- {
485
- name: "fixString",
486
- description: "FIX message string to parse",
487
- required: true
488
- }
489
- ]
490
- },
491
- {
492
- name: "newOrderSingle",
493
- description: "Creates and sends a New Order Single. IMPORTANT: All parameters must be explicitly provided by the user - no assumptions will be made.",
494
- arguments: [
495
- {
496
- name: "clOrdID",
497
- description: "Client Order ID",
498
- required: true
499
- },
500
- {
501
- name: "handlInst",
502
- description: "Handling instruction (1=Manual, 2=Automated, 3=AutomatedNoIntervention)",
503
- required: true
504
- },
505
- {
506
- name: "quantity",
507
- description: "Order quantity",
508
- required: true
509
- },
510
- {
511
- name: "price",
512
- description: "Order price",
513
- required: true
514
- },
515
- {
516
- name: "ordType",
517
- description: "Order type (1=Market, 2=Limit, 3=Stop)",
518
- required: true
519
- },
520
- {
521
- name: "side",
522
- description: "Order side (1=Buy, 2=Sell, 3=BuyMinus, 4=SellPlus, 5=SellShort, 6=SellShortExempt, 7=Undisclosed, 8=Cross, 9=CrossShort, A=CrossShortExempt, B=AsDefined, C=Opposite, D=Subscribe, E=Redeem, F=Lend, G=Borrow, H=SellUndisclosed)",
523
- required: true
524
- },
525
- {
526
- name: "symbol",
527
- description: "Trading symbol",
528
- required: true
529
- },
864
+ this.parser?.send(order);
865
+ const fixData = await response;
866
+ this.verifiedOrders.delete(args.clOrdID);
867
+ return {
868
+ content: [
530
869
  {
531
- name: "timeInForce",
532
- description: "Time in force (0=Day, 1=Good Till Cancel, 2=At Opening, 3=Immediate or Cancel, 4=Fill or Kill, 5=Good Till Crossing, 6=Good Till Date)",
533
- required: true
870
+ type: "text",
871
+ text: fixData.messageType === Messages.Reject ? `Reject message for order ${args.clOrdID}: ${JSON.stringify(fixData.toFIXJSON())}` : `Execution Report for order ${args.clOrdID}: ${JSON.stringify(fixData.toFIXJSON())}`
534
872
  }
535
873
  ]
536
- },
537
- {
538
- name: "marketDataRequest",
539
- description: "Sends a request for Market Data with the given symbol. IMPORTANT: All parameters must be explicitly provided by the user - no assumptions will be made.",
540
- arguments: [
541
- {
542
- name: "mdUpdateType",
543
- description: "Market data update type (0=FullRefresh, 1=IncrementalRefresh)",
544
- required: true
545
- },
546
- {
547
- name: "symbol",
548
- description: "Trading symbol",
549
- required: true
550
- },
551
- {
552
- name: "mdReqID",
553
- description: "Market data request ID",
554
- required: true
555
- },
556
- {
557
- name: "subscriptionRequestType",
558
- description: "Subscription request type (0=Snapshot + Updates, 1=Snapshot, 2=Unsubscribe)",
559
- required: true
560
- },
874
+ };
875
+ } catch (error) {
876
+ return {
877
+ content: [
561
878
  {
562
- name: "mdEntryType",
563
- description: "Market data entry type (0=Bid, 1=Offer, 2=Trade, 3=Index Value, 4=Opening Price)",
564
- required: true
879
+ type: "text",
880
+ text: `Error: ${error instanceof Error ? error.message : "Failed to execute order"}`
565
881
  }
566
- ]
882
+ ],
883
+ isError: true
884
+ };
885
+ }
886
+ }
887
+ );
888
+ this.server.setRequestHandler(
889
+ z.object({
890
+ method: z.literal("marketDataRequest"),
891
+ params: marketDataRequestSchema
892
+ }),
893
+ async (request, extra) => {
894
+ try {
895
+ const args = request.params;
896
+ const response = new Promise((resolve) => {
897
+ this.pendingRequests.set(args.mdReqID, resolve);
898
+ });
899
+ const messageFields = [
900
+ new Field(Fields.MsgType, Messages.MarketDataRequest),
901
+ new Field(Fields.SenderCompID, this.parser?.sender),
902
+ new Field(Fields.MsgSeqNum, this.parser?.getNextTargetMsgSeqNum()),
903
+ new Field(Fields.TargetCompID, this.parser?.target),
904
+ new Field(Fields.SendingTime, this.parser?.getTimestamp()),
905
+ new Field(Fields.MDReqID, args.mdReqID),
906
+ new Field(Fields.SubscriptionRequestType, args.subscriptionRequestType),
907
+ new Field(Fields.MarketDepth, 0),
908
+ new Field(Fields.MDUpdateType, args.mdUpdateType)
909
+ ];
910
+ messageFields.push(new Field(Fields.NoRelatedSym, args.symbols.length));
911
+ args.symbols.forEach((symbol) => {
912
+ messageFields.push(new Field(Fields.Symbol, symbol));
913
+ });
914
+ messageFields.push(new Field(Fields.NoMDEntryTypes, args.mdEntryTypes.length));
915
+ args.mdEntryTypes.forEach((entryType) => {
916
+ messageFields.push(new Field(Fields.MDEntryType, entryType));
917
+ });
918
+ const mdr = this.parser?.createMessage(...messageFields);
919
+ if (!this.parser?.connected) {
920
+ return {
921
+ content: [
922
+ {
923
+ type: "text",
924
+ text: "Error: Not connected. Ignoring message."
925
+ }
926
+ ],
927
+ isError: true
928
+ };
567
929
  }
568
- ]
569
- };
570
- });
571
- this.server.setRequestHandler(GetPromptRequestSchema, async (request) => {
572
- const { name, arguments: args } = request.params;
573
- switch (name) {
574
- case "parse": {
575
- const fixString = args?.fixString || "";
930
+ this.parser?.send(mdr);
931
+ const fixData = await response;
576
932
  return {
577
- messages: [
933
+ content: [
578
934
  {
579
- role: "user",
580
- content: {
581
- type: "text",
582
- text: `Please parse and explain this FIX message: ${fixString}`
583
- }
935
+ type: "text",
936
+ text: `Market data for ${args.symbols.join(", ")}: ${JSON.stringify(fixData.toFIXJSON())}`
584
937
  }
585
938
  ]
586
939
  };
587
- }
588
- case "parseToJSON": {
589
- const fixString = args?.fixString || "";
940
+ } catch (error) {
590
941
  return {
591
- messages: [
942
+ content: [
592
943
  {
593
- role: "user",
594
- content: {
595
- type: "text",
596
- text: `Please parse the FIX message to JSON: ${fixString}`
597
- }
944
+ type: "text",
945
+ text: `Error: ${error instanceof Error ? error.message : "Failed to request market data"}`
598
946
  }
599
- ]
947
+ ],
948
+ isError: true
600
949
  };
601
950
  }
602
- case "newOrderSingle": {
603
- const { clOrdID, handlInst, quantity, price, ordType, side, symbol, timeInForce } = args || {};
951
+ }
952
+ );
953
+ this.server.setRequestHandler(
954
+ z.object({
955
+ method: z.literal("stockGraph"),
956
+ params: symbolSchema
957
+ }),
958
+ async (request, extra) => {
959
+ this.parser?.logger.log({
960
+ level: "info",
961
+ message: "MCP Server Resource called: stockGraph"
962
+ });
963
+ const args = request.params;
964
+ const symbol = args.symbol;
965
+ const priceHistory = this.marketDataPrices.get(symbol) || [];
966
+ if (priceHistory.length === 0) {
604
967
  return {
605
- messages: [
968
+ content: [
606
969
  {
607
- role: "user",
608
- content: {
609
- type: "text",
610
- text: [
611
- "Create a New Order Single FIX message with the following parameters:",
612
- `- ClOrdID: ${clOrdID}`,
613
- `- HandlInst: ${handlInst ?? "3"} (IMPORTANT: Use the numeric/alphabetic value, not the descriptive name. For example, use '1' for Manual, '2' for Automated, '3' for AutomatedNoIntervention)`,
614
- `- Quantity: ${quantity}`,
615
- `- Price: ${price}`,
616
- `- OrdType: ${ordType ?? "1"} (IMPORTANT: Use the numeric/alphabetic value, not the descriptive name. For example, use '1' for Market, '2' for Limit, '3' for Stop)`,
617
- `- Side: ${side} (IMPORTANT: Use the numeric/alphabetic value, not the descriptive name. For example, use '1' for Buy, '2' for Sell)`,
618
- `- Symbol: ${symbol}`,
619
- `- TimeInForce: ${timeInForce ?? "0"} (IMPORTANT: Use the numeric/alphabetic value, not the descriptive name. For example, use '0' for Day, '1' for Good Till Cancel, '2' for At Opening, '3' for Immediate or Cancel, '4' for Fill or Kill, '5' for Good Till Crossing, '6' for Good Till Date)`,
620
- "",
621
- 'Note: For the OrdType parameter, always use the numeric/alphabetic value (e.g., "1" for Market, "2" for Limit, "3" for Stop) as defined in the FIX protocol, not the descriptive name.',
622
- 'Note: For the TimeInForce parameter, always use the numeric/alphabetic value (e.g., "0" for Day, "1" for Good Till Cancel, "2" for At Opening) as defined in the FIX protocol, not the descriptive name.',
623
- "",
624
- "IMPORTANT: All parameters must be set before sending the order. Missing or invalid parameters will result in a Reject message.",
625
- "",
626
- "IMPORTANT: The response will be either:",
627
- "1. An Execution Report (MsgType=8) if the order was successfully placed",
628
- "2. A Reject message (MsgType=3) if the order failed to execute (e.g., due to missing or invalid parameters)"
629
- ].join("\n")
630
- }
970
+ type: "text",
971
+ text: `No price data available for ${symbol}`
631
972
  }
632
973
  ]
633
974
  };
634
975
  }
635
- case "marketDataRequest": {
636
- const { mdUpdateType, symbol, mdReqID, subscriptionRequestType, mdEntryType } = args || {};
976
+ const width = 600;
977
+ const height = 300;
978
+ const padding = 40;
979
+ const xScale = (width - 2 * padding) / (priceHistory.length - 1);
980
+ const yMin = Math.min(...priceHistory.map((d) => d.price));
981
+ const yMax = Math.max(...priceHistory.map((d) => d.price));
982
+ const yScale = (height - 2 * padding) / (yMax - yMin);
983
+ const points = priceHistory.map((d, i) => {
984
+ const x = padding + i * xScale;
985
+ const y = height - padding - (d.price - yMin) * yScale;
986
+ return `${x},${y}`;
987
+ }).join(" L ");
988
+ const svg = `<?xml version="1.0" encoding="UTF-8"?>
989
+ <svg width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg">
990
+ <!-- Background -->
991
+ <rect width="100%" height="100%" fill="#f8f9fa"/>
992
+
993
+ <!-- Grid lines -->
994
+ <g stroke="#e9ecef" stroke-width="1">
995
+ ${Array.from({ length: 5 }, (_, i) => {
996
+ const y = padding + (height - 2 * padding) * i / 4;
997
+ return `<line x1="${padding}" y1="${y}" x2="${width - padding}" y2="${y}"/>`;
998
+ }).join("\n")}
999
+ </g>
1000
+
1001
+ <!-- Price line -->
1002
+ <path d="M ${points}"
1003
+ fill="none"
1004
+ stroke="#007bff"
1005
+ stroke-width="2"/>
1006
+
1007
+ <!-- Data points -->
1008
+ ${priceHistory.map((d, i) => {
1009
+ const x = padding + i * xScale;
1010
+ const y = height - padding - (d.price - yMin) * yScale;
1011
+ return `<circle cx="${x}" cy="${y}" r="3" fill="#007bff"/>`;
1012
+ }).join("\n")}
1013
+
1014
+ <!-- Labels -->
1015
+ <g font-family="Arial" font-size="12" fill="#495057">
1016
+ ${Array.from({ length: 5 }, (_, i) => {
1017
+ const x = padding + (width - 2 * padding) * i / 4;
1018
+ const index = Math.floor((priceHistory.length - 1) * i / 4);
1019
+ const timestamp = new Date(priceHistory[index].timestamp).toLocaleTimeString();
1020
+ return `<text x="${x + padding}" y="${height - padding + 20}" text-anchor="middle">${timestamp}</text>`;
1021
+ }).join("\n")}
1022
+ ${Array.from({ length: 5 }, (_, i) => {
1023
+ const y = padding + (height - 2 * padding) * i / 4;
1024
+ const price = yMax - (yMax - yMin) * i / 4;
1025
+ return `<text x="${padding - 5}" y="${y + 4}" text-anchor="end">$${price.toFixed(2)}</text>`;
1026
+ }).join("\n")}
1027
+ </g>
1028
+
1029
+ <!-- Title -->
1030
+ <text x="${width / 2}" y="${padding / 2}"
1031
+ font-family="Arial" font-size="16" font-weight="bold"
1032
+ text-anchor="middle" fill="#212529">
1033
+ ${symbol} - Price Chart (${priceHistory.length} points)
1034
+ </text>
1035
+ </svg>`;
1036
+ return {
1037
+ content: [
1038
+ {
1039
+ type: "text",
1040
+ text: svg
1041
+ }
1042
+ ]
1043
+ };
1044
+ }
1045
+ );
1046
+ this.server.setRequestHandler(
1047
+ z.object({
1048
+ method: z.literal("stockPriceHistory"),
1049
+ params: symbolSchema
1050
+ }),
1051
+ async (request, extra) => {
1052
+ this.parser?.logger.log({
1053
+ level: "info",
1054
+ message: "MCP Server Resource called: stockPriceHistory"
1055
+ });
1056
+ const args = request.params;
1057
+ const symbol = args.symbol;
1058
+ const priceHistory = this.marketDataPrices.get(symbol) || [];
1059
+ if (priceHistory.length === 0) {
637
1060
  return {
638
- messages: [
1061
+ content: [
639
1062
  {
640
- role: "user",
641
- content: {
642
- type: "text",
643
- text: [
644
- "Create a Market Data Request FIX message with the following parameters:",
645
- `- MDUpdateType: ${mdUpdateType ?? "0"} (IMPORTANT: Use the numeric/alphabetic value, not the descriptive name. For example, use '0' for FullRefresh, '1' for IncrementalRefresh)`,
646
- `- Symbol: ${symbol}`,
647
- `- MDReqID: ${mdReqID}`,
648
- `- SubscriptionRequestType: ${subscriptionRequestType ?? "0"} (IMPORTANT: Use the numeric/alphabetic value, not the descriptive name. For example, use '0' for Snapshot + Updates, '1' for Snapshot, '2' for Unsubscribe)`,
649
- `- MDEntryType: ${mdEntryType ?? "0"} (IMPORTANT: Use the numeric/alphabetic value, not the descriptive name. For example, use '0' for Bid, '1' for Offer, '2' for Trade, '3' for Index Value, '4' for Opening Price)`,
650
- "",
651
- "Format the response as a JSON object with FIX tag numbers as keys and their corresponding values.",
652
- "",
653
- 'Note: For the MDUpdateType parameter, always use the numeric/alphabetic value (e.g., "0" for FullRefresh, "1" for IncrementalRefresh) as defined in the FIX protocol, not the descriptive name.',
654
- 'Note: For the SubscriptionRequestType parameter, always use the numeric/alphabetic value (e.g., "0" for Snapshot + Updates, "1" for Snapshot, "2" for Unsubscribe) as defined in the FIX protocol, not the descriptive name.',
655
- 'Note: For the MDEntryType parameter, always use the numeric/alphabetic value (e.g., "0" for Bid, "1" for Offer, "2" for Trade, "3" for Index Value, "4" for Opening Price) as defined in the FIX protocol, not the descriptive name.'
656
- ].join("\n")
657
- }
1063
+ type: "text",
1064
+ text: `No price data available for ${symbol}`
658
1065
  }
659
1066
  ]
660
1067
  };
661
1068
  }
662
- default:
663
- throw new Error(`Unknown prompt: ${name}`);
1069
+ return {
1070
+ content: [
1071
+ {
1072
+ type: "text",
1073
+ text: JSON.stringify(
1074
+ {
1075
+ symbol,
1076
+ count: priceHistory.length,
1077
+ prices: priceHistory.map((point) => ({
1078
+ timestamp: new Date(point.timestamp).toISOString(),
1079
+ price: point.price
1080
+ }))
1081
+ },
1082
+ null,
1083
+ 2
1084
+ )
1085
+ }
1086
+ ]
1087
+ };
664
1088
  }
665
- });
1089
+ );
666
1090
  process.on("SIGINT", async () => {
667
1091
  await this.server.close();
668
1092
  process.exit(0);