fixparser-plugin-mcp 9.1.7-b2f9f891 → 9.1.7-b6430f19

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