-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime.js
More file actions
157 lines (143 loc) · 3.87 KB
/
time.js
File metadata and controls
157 lines (143 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import * as chrono from "chrono-node";
import { addDays } from "date-fns/addDays";
import { addMonths } from "date-fns/addMonths";
import { subDays } from "date-fns/subDays";
import { z } from "zod";
export class TimeRange {
static parse = (range) => {
if (typeof range === "object") {
range = range.time_range;
}
if (!range) {
return undefined;
}
const parsed = chrono.parse(range);
const [chronoRange] = parsed;
if (!chronoRange) {
return undefined;
}
return new TimeRange(chronoRange);
};
constructor(range) {
this.range = range;
}
get hasAfter() {
return !!this.getAfter();
}
/**
* Gets after date in ISO format.
* @param {ITimeBoundOptions} [options] - Options for time bound calculation
* @returns {string|undefined}
*/
getAfter = (options) => {
if (!this.range.start) {
return undefined;
}
let after = this.range.start.date();
if (this.range.start.isCertain("hour") && !options?.ignoreTime) {
return after.toISOString();
}
if (options?.exclusive) {
after = subDays(after, 1);
}
return after.toISOString().split("T")[0];
};
get hasBefore() {
return !!this.getBefore();
}
/**
* Gets the before date in ISO format.
* @param {ITimeBoundOptions} [options] - Options for time bound calculation
* @returns {string|undefined}
*/
getBefore = (options) => {
let before = this.range.end?.date();
if (
!before &&
this.range.start?.isCertain("month") &&
!this.range.start.isCertain("day")
) {
before = subDays(addMonths(this.range.start.date(), 1), 1);
}
if (!before) {
return undefined;
}
if (this.range.end?.isCertain("hour") && !options?.ignoreTime) {
return before.toISOString();
}
if (options?.exclusive) {
before = addDays(before, 1);
}
return before.toISOString().split("T")[0];
};
}
const main = async () => {
try {
const server = new McpServer({
name: "Time",
version: "1.0.0",
});
server.tool("get_current_time", "Get the current time", {}, async () => {
const now = new Date();
return {
content: [
{
type: "text",
text: JSON.stringify({
time_millis: now.getTime(),
time_iso: now.toISOString(),
}),
},
],
};
});
server.tool(
"resolve_time_description",
"Resolve a timestamp or time range from natural language time description",
{
time_range: z
.string()
.describe(
`Text describing the date range to search within, which will be parsed by chrono-node.
e.g. "last week", "yesterday", "17 Aug - 19 Aug", etc.
If not provided, will search from the beginning of time.`
)
.optional(),
},
async (params) => {
const timeRange = TimeRange.parse(params);
if (!timeRange) {
return {
content: [
{
type: "text",
text: params.time_range
? "Invalid time range"
: "No time range provided",
},
],
};
}
return {
content: [
{
type: "text",
text: JSON.stringify({
after: timeRange.getAfter(params),
before: timeRange.getBefore(params),
}),
},
],
};
}
);
const stdioTransport = new StdioServerTransport();
await server.connect(stdioTransport);
console.error("Time MCP server started");
} catch (error) {
console.error(`Error starting Time MCP server: ${error}`);
}
};
main();