@hasna/logs 0.3.11 → 0.3.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +170 -0
- package/dist/cli/index.js +2 -2
- package/dist/index-ww5ggfv3.js +90 -0
- package/dist/mcp/index.js +5 -4
- package/dist/query-7jwj05er.js +15 -0
- package/dist/server/index.js +1 -1
- package/package.json +4 -4
- package/src/lib/query.ts +23 -3
- package/src/mcp/index.test.ts +25 -0
- package/src/mcp/index.ts +46 -35
package/LICENSE
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of recording and
|
|
55
|
+
discussing the Work, but excluding communication that is conspicuously
|
|
56
|
+
marked or designated in writing by the copyright owner as "Not a
|
|
57
|
+
Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and included
|
|
61
|
+
within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent contributions
|
|
76
|
+
Licensors can make available under this License.
|
|
77
|
+
|
|
78
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
79
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
80
|
+
modifications, and in Source or Object form, provided that You
|
|
81
|
+
meet the following conditions:
|
|
82
|
+
|
|
83
|
+
(a) You must give any other recipients of the Work or Derivative Works
|
|
84
|
+
a copy of this License; and
|
|
85
|
+
|
|
86
|
+
(b) You must cause any modified files to carry prominent notices
|
|
87
|
+
stating that You changed the files; and
|
|
88
|
+
|
|
89
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
90
|
+
that You distribute, all copyright, patent, trademark, and
|
|
91
|
+
attribution notices from the Source form of the Work,
|
|
92
|
+
excluding those notices that do not pertain to any part of
|
|
93
|
+
the Derivative Works; and
|
|
94
|
+
|
|
95
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
96
|
+
distribution, You must include a readable copy of the attribution
|
|
97
|
+
notices contained within such NOTICE file, in at least one
|
|
98
|
+
of the following places: within a NOTICE text file distributed
|
|
99
|
+
as part of the Derivative Works; within the Source form or
|
|
100
|
+
documentation, if provided along with the Derivative Works; or,
|
|
101
|
+
within a display generated by the Derivative Works, if and
|
|
102
|
+
wherever such third-party notices normally appear. The contents
|
|
103
|
+
of the NOTICE file are for informational purposes only and
|
|
104
|
+
do not modify the License. You may add Your own attribution
|
|
105
|
+
notices within Derivative Works that You distribute, alongside
|
|
106
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
107
|
+
that such additional attribution notices cannot be construed
|
|
108
|
+
as modifying the License.
|
|
109
|
+
|
|
110
|
+
You may add Your own license statement for Your modifications and
|
|
111
|
+
may provide additional grant of rights to use, copy, modify, merge,
|
|
112
|
+
publish, distribute, sublicense, and/or sell copies of the Software.
|
|
113
|
+
|
|
114
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
115
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
116
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
117
|
+
this License, without any additional terms or conditions.
|
|
118
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
119
|
+
the terms of any separate license agreement you may have executed
|
|
120
|
+
with Licensor regarding such Contributions.
|
|
121
|
+
|
|
122
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
123
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
124
|
+
except as required for reasonable and customary use in describing the
|
|
125
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
126
|
+
|
|
127
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
128
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
129
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
130
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
131
|
+
implied, including, without limitation, any warranties or conditions
|
|
132
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
133
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
134
|
+
appropriateness of using or redistributing the Work and assume any
|
|
135
|
+
risks associated with Your exercise of permissions under this License.
|
|
136
|
+
|
|
137
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
138
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
139
|
+
unless required by applicable law (such as deliberate and grossly
|
|
140
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
141
|
+
liable to You for damages, including any direct, indirect, special,
|
|
142
|
+
incidental, or exemplary damages of any character arising as a
|
|
143
|
+
result of this License or out of the use or inability to use the
|
|
144
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
145
|
+
work stoppage, computer failure or malfunction, or all other
|
|
146
|
+
commercial damages or losses), even if such Contributor has been
|
|
147
|
+
advised of the possibility of such damages.
|
|
148
|
+
|
|
149
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
150
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
151
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
152
|
+
or other liability obligations and/or rights consistent with this
|
|
153
|
+
License. However, in accepting such obligations, You may offer only
|
|
154
|
+
conditions consistent with this License.
|
|
155
|
+
|
|
156
|
+
END OF TERMS AND CONDITIONS
|
|
157
|
+
|
|
158
|
+
Copyright 2024 hasna.com
|
|
159
|
+
|
|
160
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
161
|
+
you may not use this file except in compliance with the License.
|
|
162
|
+
You may obtain a copy of the License at
|
|
163
|
+
|
|
164
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
165
|
+
|
|
166
|
+
Unless required by applicable law or agreed to in writing, software
|
|
167
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
168
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
169
|
+
See the License for the specific language governing permissions and
|
|
170
|
+
limitations under the License.
|
package/dist/cli/index.js
CHANGED
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
import {
|
|
21
21
|
searchLogs,
|
|
22
22
|
tailLogs
|
|
23
|
-
} from "../index-
|
|
23
|
+
} from "../index-ww5ggfv3.js";
|
|
24
24
|
import"../index-997bkzr2.js";
|
|
25
25
|
import {
|
|
26
26
|
__commonJS,
|
|
@@ -2323,7 +2323,7 @@ ${C.bold}Perf Regressions:${C.reset}`);
|
|
|
2323
2323
|
});
|
|
2324
2324
|
program2.command("watch").description("Stream new logs in real time with color coding").option("--project <name|id>", "Filter by project name or ID").option("--level <levels>", "Comma-separated levels (debug,info,warn,error,fatal)").option("--service <name>", "Filter by service name").option("--interval <ms>", "Poll interval in milliseconds (default: 500)", "500").option("--since <time>", "Start from this time (default: now)").action(async (opts) => {
|
|
2325
2325
|
const db = getDb();
|
|
2326
|
-
const { searchLogs: searchLogs2 } = await import("../query-
|
|
2326
|
+
const { searchLogs: searchLogs2 } = await import("../query-7jwj05er.js");
|
|
2327
2327
|
let projectId = opts.project;
|
|
2328
2328
|
if (projectId) {
|
|
2329
2329
|
const proj = db.query("SELECT id FROM projects WHERE id = ? OR name = ?").get(projectId, projectId);
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
parseTime
|
|
4
|
+
} from "./index-997bkzr2.js";
|
|
5
|
+
|
|
6
|
+
// src/lib/query.ts
|
|
7
|
+
function searchLogs(db, q) {
|
|
8
|
+
const conditions = [];
|
|
9
|
+
const params = {};
|
|
10
|
+
if (q.project_id) {
|
|
11
|
+
conditions.push("l.project_id = $project_id");
|
|
12
|
+
params.$project_id = q.project_id;
|
|
13
|
+
}
|
|
14
|
+
if (q.page_id) {
|
|
15
|
+
conditions.push("l.page_id = $page_id");
|
|
16
|
+
params.$page_id = q.page_id;
|
|
17
|
+
}
|
|
18
|
+
if (q.service) {
|
|
19
|
+
conditions.push("l.service = $service");
|
|
20
|
+
params.$service = q.service;
|
|
21
|
+
}
|
|
22
|
+
if (q.trace_id) {
|
|
23
|
+
conditions.push("l.trace_id = $trace_id");
|
|
24
|
+
params.$trace_id = q.trace_id;
|
|
25
|
+
}
|
|
26
|
+
if (q.since) {
|
|
27
|
+
conditions.push("l.timestamp >= $since");
|
|
28
|
+
params.$since = parseTime(q.since) ?? q.since;
|
|
29
|
+
}
|
|
30
|
+
if (q.until) {
|
|
31
|
+
conditions.push("l.timestamp <= $until");
|
|
32
|
+
params.$until = parseTime(q.until) ?? q.until;
|
|
33
|
+
}
|
|
34
|
+
if (q.level) {
|
|
35
|
+
const levels = Array.isArray(q.level) ? q.level : [q.level];
|
|
36
|
+
const placeholders = levels.map((_, i) => `$level${i}`).join(",");
|
|
37
|
+
levels.forEach((lv, i) => {
|
|
38
|
+
params[`$level${i}`] = lv;
|
|
39
|
+
});
|
|
40
|
+
conditions.push(`l.level IN (${placeholders})`);
|
|
41
|
+
}
|
|
42
|
+
const limit = q.limit ?? 100;
|
|
43
|
+
const offset = q.offset ?? 0;
|
|
44
|
+
params.$limit = limit;
|
|
45
|
+
params.$offset = offset;
|
|
46
|
+
if (q.text) {
|
|
47
|
+
params.$text = q.text;
|
|
48
|
+
const where2 = conditions.length ? `WHERE ${conditions.join(" AND ")} AND` : "WHERE";
|
|
49
|
+
const sql2 = `
|
|
50
|
+
SELECT l.* FROM logs l
|
|
51
|
+
${where2} l.rowid IN (SELECT rowid FROM logs_fts WHERE logs_fts MATCH $text)
|
|
52
|
+
ORDER BY l.timestamp DESC
|
|
53
|
+
LIMIT $limit OFFSET $offset
|
|
54
|
+
`;
|
|
55
|
+
return db.prepare(sql2).all(params);
|
|
56
|
+
}
|
|
57
|
+
const where = conditions.length ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
58
|
+
const sql = `SELECT * FROM logs l ${where} ORDER BY l.timestamp DESC LIMIT $limit OFFSET $offset`;
|
|
59
|
+
return db.prepare(sql).all(params);
|
|
60
|
+
}
|
|
61
|
+
function tailLogs(db, projectId, n = 50) {
|
|
62
|
+
if (projectId) {
|
|
63
|
+
return db.prepare("SELECT * FROM logs WHERE project_id = $p ORDER BY timestamp DESC LIMIT $n").all({ $p: projectId, $n: n });
|
|
64
|
+
}
|
|
65
|
+
return db.prepare("SELECT * FROM logs ORDER BY timestamp DESC LIMIT $n").all({ $n: n });
|
|
66
|
+
}
|
|
67
|
+
function getLogContext(db, traceId) {
|
|
68
|
+
return db.prepare("SELECT * FROM logs WHERE trace_id = $t ORDER BY timestamp ASC").all({ $t: traceId });
|
|
69
|
+
}
|
|
70
|
+
function getLogContextFromId(db, logId, window = 0) {
|
|
71
|
+
const log = db.prepare("SELECT * FROM logs WHERE id = $id").get({ $id: logId });
|
|
72
|
+
if (!log)
|
|
73
|
+
return [];
|
|
74
|
+
const trace = log.trace_id ? getLogContext(db, log.trace_id) : [log];
|
|
75
|
+
if (window <= 0)
|
|
76
|
+
return trace;
|
|
77
|
+
const before = db.prepare(`SELECT * FROM logs WHERE id != $id AND timestamp <= $ts ORDER BY timestamp DESC LIMIT $n`).all({ $id: logId, $ts: log.timestamp, $n: window });
|
|
78
|
+
const after = db.prepare(`SELECT * FROM logs WHERE id != $id AND timestamp > $ts ORDER BY timestamp ASC LIMIT $n`).all({ $id: logId, $ts: log.timestamp, $n: window });
|
|
79
|
+
const seen = new Set;
|
|
80
|
+
const merged = [];
|
|
81
|
+
for (const row of [...before.reverse(), ...trace, ...after]) {
|
|
82
|
+
if (!seen.has(row.id)) {
|
|
83
|
+
seen.add(row.id);
|
|
84
|
+
merged.push(row);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return merged.sort((a, b) => a.timestamp < b.timestamp ? -1 : a.timestamp > b.timestamp ? 1 : 0);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export { searchLogs, tailLogs, getLogContext, getLogContextFromId };
|
package/dist/mcp/index.js
CHANGED
|
@@ -34,7 +34,7 @@ import {
|
|
|
34
34
|
getLogContextFromId,
|
|
35
35
|
searchLogs,
|
|
36
36
|
tailLogs
|
|
37
|
-
} from "../index-
|
|
37
|
+
} from "../index-ww5ggfv3.js";
|
|
38
38
|
import {
|
|
39
39
|
countLogs
|
|
40
40
|
} from "../index-edn08m6f.js";
|
|
@@ -28660,9 +28660,10 @@ server.tool("log_context", {
|
|
|
28660
28660
|
}));
|
|
28661
28661
|
server.tool("log_context_from_id", {
|
|
28662
28662
|
log_id: exports_external.string(),
|
|
28663
|
-
brief: exports_external.boolean().optional()
|
|
28664
|
-
|
|
28665
|
-
|
|
28663
|
+
brief: exports_external.boolean().optional(),
|
|
28664
|
+
window: exports_external.number().int().min(0).optional().describe("Return N logs before and after the target log's timestamp (in addition to trace context)")
|
|
28665
|
+
}, ({ log_id, brief, window }) => ({
|
|
28666
|
+
content: [{ type: "text", text: JSON.stringify(applyBrief(getLogContextFromId(db, log_id, window ?? 0), brief !== false)) }]
|
|
28666
28667
|
}));
|
|
28667
28668
|
server.tool("log_export", {
|
|
28668
28669
|
project_id: exports_external.string().optional().describe("Project name or ID"),
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
getLogContext,
|
|
4
|
+
getLogContextFromId,
|
|
5
|
+
searchLogs,
|
|
6
|
+
tailLogs
|
|
7
|
+
} from "./index-ww5ggfv3.js";
|
|
8
|
+
import"./index-997bkzr2.js";
|
|
9
|
+
import"./index-re3ntm60.js";
|
|
10
|
+
export {
|
|
11
|
+
tailLogs,
|
|
12
|
+
searchLogs,
|
|
13
|
+
getLogContextFromId,
|
|
14
|
+
getLogContext
|
|
15
|
+
};
|
package/dist/server/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hasna/logs",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.13",
|
|
4
4
|
"description": "Log aggregation + browser script + headless page scanner + performance monitoring for AI agents",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"url": "https://github.com/hasna/logs.git"
|
|
25
25
|
},
|
|
26
26
|
"publishConfig": {
|
|
27
|
-
"
|
|
28
|
-
"
|
|
27
|
+
"registry": "https://registry.npmjs.org",
|
|
28
|
+
"access": "public"
|
|
29
29
|
},
|
|
30
30
|
"keywords": [
|
|
31
31
|
"logs",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"lighthouse"
|
|
38
38
|
],
|
|
39
39
|
"author": "Andrei Hasna <andrei@hasna.com>",
|
|
40
|
-
"license": "
|
|
40
|
+
"license": "Apache-2.0",
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@modelcontextprotocol/sdk": "^1.12.1",
|
|
43
43
|
"commander": "^14.0.0",
|
package/src/lib/query.ts
CHANGED
|
@@ -56,9 +56,29 @@ export function getLogContext(db: Database, traceId: string): LogRow[] {
|
|
|
56
56
|
.all({ $t: traceId }) as LogRow[]
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
export function getLogContextFromId(db: Database, logId: string): LogRow[] {
|
|
59
|
+
export function getLogContextFromId(db: Database, logId: string, window = 0): LogRow[] {
|
|
60
60
|
const log = db.prepare("SELECT * FROM logs WHERE id = $id").get({ $id: logId }) as LogRow | null
|
|
61
61
|
if (!log) return []
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
|
|
63
|
+
// Get trace context (or just the log itself if no trace)
|
|
64
|
+
const trace: LogRow[] = log.trace_id ? getLogContext(db, log.trace_id) : [log]
|
|
65
|
+
|
|
66
|
+
if (window <= 0) return trace
|
|
67
|
+
|
|
68
|
+
// Fetch N rows before and after the target log's timestamp
|
|
69
|
+
const before = db.prepare(
|
|
70
|
+
`SELECT * FROM logs WHERE id != $id AND timestamp <= $ts ORDER BY timestamp DESC LIMIT $n`
|
|
71
|
+
).all({ $id: logId, $ts: log.timestamp, $n: window }) as LogRow[]
|
|
72
|
+
|
|
73
|
+
const after = db.prepare(
|
|
74
|
+
`SELECT * FROM logs WHERE id != $id AND timestamp > $ts ORDER BY timestamp ASC LIMIT $n`
|
|
75
|
+
).all({ $id: logId, $ts: log.timestamp, $n: window }) as LogRow[]
|
|
76
|
+
|
|
77
|
+
// Merge: before (oldest first) + trace + after, deduplicate by id
|
|
78
|
+
const seen = new Set<string>()
|
|
79
|
+
const merged: LogRow[] = []
|
|
80
|
+
for (const row of [...before.reverse(), ...trace, ...after]) {
|
|
81
|
+
if (!seen.has(row.id)) { seen.add(row.id); merged.push(row) }
|
|
82
|
+
}
|
|
83
|
+
return merged.sort((a, b) => a.timestamp < b.timestamp ? -1 : a.timestamp > b.timestamp ? 1 : 0)
|
|
64
84
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { test, expect } from "bun:test"
|
|
2
|
+
import { fileURLToPath } from "url"
|
|
3
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
|
|
4
|
+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"
|
|
5
|
+
|
|
6
|
+
test("logs MCP lists tools over stdio", async () => {
|
|
7
|
+
const entry = fileURLToPath(new URL("./index.ts", import.meta.url))
|
|
8
|
+
const transport = new StdioClientTransport({
|
|
9
|
+
command: "bun",
|
|
10
|
+
args: ["run", entry],
|
|
11
|
+
})
|
|
12
|
+
const client = new Client({ name: "logs-mcp-test", version: "0.0.0" }, { capabilities: {} })
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
await client.connect(transport)
|
|
16
|
+
const result = await client.listTools()
|
|
17
|
+
const toolNames = result.tools.map((tool) => tool.name)
|
|
18
|
+
|
|
19
|
+
expect(toolNames.length).toBeGreaterThan(0)
|
|
20
|
+
expect(toolNames).toContain("get_health")
|
|
21
|
+
expect(toolNames).toContain("log_search")
|
|
22
|
+
} finally {
|
|
23
|
+
await client.close().catch(() => {})
|
|
24
|
+
}
|
|
25
|
+
})
|
package/src/mcp/index.ts
CHANGED
|
@@ -75,53 +75,62 @@ const TOOLS: Record<string, { desc: string; params: string }> = {
|
|
|
75
75
|
describe_tools: { desc: "List all tools with descriptions and param signatures", params: "()" },
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
// Fellow agents: keep MCP registrations behind this helper so descriptions and schemas stay aligned with the current SDK.
|
|
79
|
+
function registerTool(
|
|
80
|
+
name: keyof typeof TOOLS,
|
|
81
|
+
schema: Record<string, z.ZodTypeAny>,
|
|
82
|
+
handler: (...args: any[]) => any,
|
|
83
|
+
) {
|
|
84
|
+
return server.tool(name, TOOLS[name].desc, schema, handler)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
registerTool("search_tools", { query: z.string() }, ({ query }) => {
|
|
79
88
|
const q = query.toLowerCase()
|
|
80
89
|
const matches = Object.entries(TOOLS).filter(([k, v]) => k.includes(q) || v.desc.toLowerCase().includes(q))
|
|
81
90
|
const text = matches.map(([k, v]) => `${k}${v.params} — ${v.desc}`).join("\n") || "No matches"
|
|
82
91
|
return { content: [{ type: "text", text }] }
|
|
83
92
|
})
|
|
84
93
|
|
|
85
|
-
|
|
94
|
+
registerTool("describe_tools", {}, () => ({
|
|
86
95
|
content: [{ type: "text", text: Object.entries(TOOLS).map(([k, v]) => `${k}${v.params} — ${v.desc}`).join("\n") }]
|
|
87
96
|
}))
|
|
88
97
|
|
|
89
|
-
|
|
98
|
+
registerTool("resolve_project", { name: z.string() }, ({ name }) => {
|
|
90
99
|
const id = resolveProjectId(db, name)
|
|
91
100
|
const project = id ? db.prepare("SELECT * FROM projects WHERE id = $id").get({ $id: id }) : null
|
|
92
101
|
return { content: [{ type: "text", text: JSON.stringify(project ?? { error: `Project '${name}' not found` }) }] }
|
|
93
102
|
})
|
|
94
103
|
|
|
95
|
-
|
|
104
|
+
registerTool("register_project", {
|
|
96
105
|
name: z.string(), github_repo: z.string().optional(), base_url: z.string().optional(), description: z.string().optional(),
|
|
97
106
|
}, (args) => ({ content: [{ type: "text", text: JSON.stringify(createProject(db, args)) }] }))
|
|
98
107
|
|
|
99
|
-
|
|
108
|
+
registerTool("register_page", {
|
|
100
109
|
project_id: z.string(), url: z.string(), path: z.string().optional(), name: z.string().optional(),
|
|
101
110
|
}, (args) => ({ content: [{ type: "text", text: JSON.stringify(createPage(db, { ...args, project_id: rp(args.project_id) ?? args.project_id })) }] }))
|
|
102
111
|
|
|
103
|
-
|
|
112
|
+
registerTool("create_scan_job", {
|
|
104
113
|
project_id: z.string(), schedule: z.string(), page_id: z.string().optional(),
|
|
105
114
|
}, (args) => ({ content: [{ type: "text", text: JSON.stringify(createJob(db, { ...args, project_id: rp(args.project_id) ?? args.project_id })) }] }))
|
|
106
115
|
|
|
107
|
-
|
|
116
|
+
registerTool("log_push", {
|
|
108
117
|
level: z.enum(["debug", "info", "warn", "error", "fatal"]),
|
|
109
118
|
message: z.string(),
|
|
110
119
|
project_id: z.string().optional(), service: z.string().optional(),
|
|
111
120
|
trace_id: z.string().optional(), session_id: z.string().optional(),
|
|
112
121
|
agent: z.string().optional(), url: z.string().optional(),
|
|
113
|
-
metadata: z.record(z.unknown()).optional(),
|
|
122
|
+
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
114
123
|
}, (args) => {
|
|
115
124
|
const row = ingestLog(db, { ...args, project_id: rp(args.project_id) })
|
|
116
125
|
return { content: [{ type: "text", text: `Logged: ${row.id}` }] }
|
|
117
126
|
})
|
|
118
127
|
|
|
119
|
-
|
|
128
|
+
registerTool("log_push_batch", {
|
|
120
129
|
entries: z.array(z.object({
|
|
121
130
|
level: z.enum(["debug", "info", "warn", "error", "fatal"]),
|
|
122
131
|
message: z.string(),
|
|
123
132
|
project_id: z.string().optional(), service: z.string().optional(),
|
|
124
|
-
trace_id: z.string().optional(), metadata: z.record(z.unknown()).optional(),
|
|
133
|
+
trace_id: z.string().optional(), metadata: z.record(z.string(), z.unknown()).optional(),
|
|
125
134
|
})),
|
|
126
135
|
trace_id: z.string().optional().describe("Shared trace_id applied to all entries that don't have their own trace_id"),
|
|
127
136
|
project_id: z.string().optional().describe("Shared project_id applied to all entries (individual entry project_id takes precedence)"),
|
|
@@ -134,7 +143,7 @@ server.tool("log_push_batch", {
|
|
|
134
143
|
return { content: [{ type: "text", text: `Logged ${rows.length} entries${trace_id ? ` (trace: ${trace_id})` : ''}` }] }
|
|
135
144
|
})
|
|
136
145
|
|
|
137
|
-
|
|
146
|
+
registerTool("log_search", {
|
|
138
147
|
project_id: z.string().optional(), page_id: z.string().optional(),
|
|
139
148
|
level: z.string().optional(), service: z.string().optional(),
|
|
140
149
|
since: z.string().optional(), until: z.string().optional(),
|
|
@@ -151,14 +160,14 @@ server.tool("log_search", {
|
|
|
151
160
|
return { content: [{ type: "text", text: JSON.stringify(applyBrief(rows, args.brief !== false)) }] }
|
|
152
161
|
})
|
|
153
162
|
|
|
154
|
-
|
|
163
|
+
registerTool("log_tail", {
|
|
155
164
|
project_id: z.string().optional(), n: z.number().optional(), brief: z.boolean().optional(),
|
|
156
165
|
}, ({ project_id, n, brief }) => {
|
|
157
166
|
const rows = tailLogs(db, rp(project_id), n ?? 50)
|
|
158
167
|
return { content: [{ type: "text", text: JSON.stringify(applyBrief(rows, brief !== false)) }] }
|
|
159
168
|
})
|
|
160
169
|
|
|
161
|
-
|
|
170
|
+
registerTool("log_count", {
|
|
162
171
|
project_id: z.string().optional(), service: z.string().optional(),
|
|
163
172
|
level: z.string().optional(), since: z.string().optional(), until: z.string().optional(),
|
|
164
173
|
group_by: z.enum(["level", "service"]).optional().describe("Return breakdown by 'level' or 'service' in addition to totals"),
|
|
@@ -166,7 +175,7 @@ server.tool("log_count", {
|
|
|
166
175
|
content: [{ type: "text", text: JSON.stringify(countLogs(db, { ...args, project_id: rp(args.project_id) })) }]
|
|
167
176
|
}))
|
|
168
177
|
|
|
169
|
-
|
|
178
|
+
registerTool("log_recent_errors", {
|
|
170
179
|
project_id: z.string().optional(), since: z.string().optional(), limit: z.number().optional(),
|
|
171
180
|
}, ({ project_id, since, limit }) => {
|
|
172
181
|
const rows = searchLogs(db, {
|
|
@@ -178,25 +187,27 @@ server.tool("log_recent_errors", {
|
|
|
178
187
|
return { content: [{ type: "text", text: JSON.stringify(applyBrief(rows, true)) }] }
|
|
179
188
|
})
|
|
180
189
|
|
|
181
|
-
|
|
190
|
+
registerTool("log_summary", {
|
|
182
191
|
project_id: z.string().optional(), since: z.string().optional(),
|
|
183
192
|
}, ({ project_id, since }) => ({
|
|
184
193
|
content: [{ type: "text", text: JSON.stringify(summarizeLogs(db, rp(project_id), parseTime(since) ?? since)) }]
|
|
185
194
|
}))
|
|
186
195
|
|
|
187
|
-
|
|
196
|
+
registerTool("log_context", {
|
|
188
197
|
trace_id: z.string(), brief: z.boolean().optional(),
|
|
189
198
|
}, ({ trace_id, brief }) => ({
|
|
190
199
|
content: [{ type: "text", text: JSON.stringify(applyBrief(getLogContext(db, trace_id), brief !== false)) }]
|
|
191
200
|
}))
|
|
192
201
|
|
|
193
|
-
|
|
194
|
-
log_id: z.string(),
|
|
195
|
-
|
|
196
|
-
|
|
202
|
+
registerTool("log_context_from_id", {
|
|
203
|
+
log_id: z.string(),
|
|
204
|
+
brief: z.boolean().optional(),
|
|
205
|
+
window: z.number().int().min(0).optional().describe("Return N logs before and after the target log's timestamp (in addition to trace context)"),
|
|
206
|
+
}, ({ log_id, brief, window }) => ({
|
|
207
|
+
content: [{ type: "text", text: JSON.stringify(applyBrief(getLogContextFromId(db, log_id, window ?? 0), brief !== false)) }]
|
|
197
208
|
}))
|
|
198
209
|
|
|
199
|
-
|
|
210
|
+
registerTool("log_export", {
|
|
200
211
|
project_id: z.string().optional().describe("Project name or ID"),
|
|
201
212
|
format: z.enum(["json", "csv"]).optional().default("json").describe("Output format"),
|
|
202
213
|
since: z.string().optional().describe("Since time (1h, 24h, 7d, ISO)"),
|
|
@@ -220,7 +231,7 @@ server.tool("log_export", {
|
|
|
220
231
|
return { content: [{ type: "text" as const, text: chunks.join("") }] }
|
|
221
232
|
})
|
|
222
233
|
|
|
223
|
-
|
|
234
|
+
registerTool("log_diagnose", {
|
|
224
235
|
project_id: z.string(),
|
|
225
236
|
since: z.string().optional(),
|
|
226
237
|
include: z.array(z.enum(["top_errors", "error_rate", "failing_pages", "perf"])).optional(),
|
|
@@ -228,7 +239,7 @@ server.tool("log_diagnose", {
|
|
|
228
239
|
content: [{ type: "text", text: JSON.stringify(diagnose(db, rp(project_id) ?? project_id, since, include)) }]
|
|
229
240
|
}))
|
|
230
241
|
|
|
231
|
-
|
|
242
|
+
registerTool("log_compare", {
|
|
232
243
|
project_id: z.string(),
|
|
233
244
|
a_since: z.string(), a_until: z.string(),
|
|
234
245
|
b_since: z.string(), b_until: z.string(),
|
|
@@ -238,71 +249,71 @@ server.tool("log_compare", {
|
|
|
238
249
|
parseTime(b_since) ?? b_since, parseTime(b_until) ?? b_until)) }]
|
|
239
250
|
}))
|
|
240
251
|
|
|
241
|
-
|
|
252
|
+
registerTool("log_session_context", {
|
|
242
253
|
session_id: z.string(), brief: z.boolean().optional(),
|
|
243
254
|
}, async ({ session_id, brief }) => {
|
|
244
255
|
const ctx = await getSessionContext(db, session_id)
|
|
245
256
|
return { content: [{ type: "text", text: JSON.stringify({ ...ctx, logs: applyBrief(ctx.logs, brief !== false) }) }] }
|
|
246
257
|
})
|
|
247
258
|
|
|
248
|
-
|
|
259
|
+
registerTool("perf_snapshot", {
|
|
249
260
|
project_id: z.string(), page_id: z.string().optional(),
|
|
250
261
|
}, ({ project_id, page_id }) => {
|
|
251
262
|
const snap = getLatestSnapshot(db, rp(project_id) ?? project_id, page_id)
|
|
252
263
|
return { content: [{ type: "text", text: JSON.stringify(snap ? { ...snap, label: scoreLabel(snap.score) } : null) }] }
|
|
253
264
|
})
|
|
254
265
|
|
|
255
|
-
|
|
266
|
+
registerTool("perf_trend", {
|
|
256
267
|
project_id: z.string(), page_id: z.string().optional(), since: z.string().optional(), limit: z.number().optional(),
|
|
257
268
|
}, ({ project_id, page_id, since, limit }) => ({
|
|
258
269
|
content: [{ type: "text", text: JSON.stringify(getPerfTrend(db, rp(project_id) ?? project_id, page_id, parseTime(since) ?? since, limit ?? 50)) }]
|
|
259
270
|
}))
|
|
260
271
|
|
|
261
|
-
|
|
272
|
+
registerTool("scan_status", {
|
|
262
273
|
project_id: z.string().optional(),
|
|
263
274
|
}, ({ project_id }) => ({
|
|
264
275
|
content: [{ type: "text", text: JSON.stringify(listJobs(db, rp(project_id))) }]
|
|
265
276
|
}))
|
|
266
277
|
|
|
267
|
-
|
|
278
|
+
registerTool("list_projects", {}, () => ({
|
|
268
279
|
content: [{ type: "text", text: JSON.stringify(listProjects(db)) }]
|
|
269
280
|
}))
|
|
270
281
|
|
|
271
|
-
|
|
282
|
+
registerTool("list_pages", { project_id: z.string() }, ({ project_id }) => ({
|
|
272
283
|
content: [{ type: "text", text: JSON.stringify(listPages(db, rp(project_id) ?? project_id)) }]
|
|
273
284
|
}))
|
|
274
285
|
|
|
275
|
-
|
|
286
|
+
registerTool("list_issues", {
|
|
276
287
|
project_id: z.string().optional(), status: z.string().optional(), limit: z.number().optional(),
|
|
277
288
|
}, ({ project_id, status, limit }) => ({
|
|
278
289
|
content: [{ type: "text", text: JSON.stringify(listIssues(db, rp(project_id), status, limit ?? 50)) }]
|
|
279
290
|
}))
|
|
280
291
|
|
|
281
|
-
|
|
292
|
+
registerTool("resolve_issue", {
|
|
282
293
|
id: z.string(), status: z.enum(["open", "resolved", "ignored"]),
|
|
283
294
|
}, ({ id, status }) => ({
|
|
284
295
|
content: [{ type: "text", text: JSON.stringify(updateIssueStatus(db, id, status)) }]
|
|
285
296
|
}))
|
|
286
297
|
|
|
287
|
-
|
|
298
|
+
registerTool("create_alert_rule", {
|
|
288
299
|
project_id: z.string(), name: z.string(),
|
|
289
300
|
level: z.string().optional(), service: z.string().optional(),
|
|
290
301
|
threshold_count: z.number().optional(), window_seconds: z.number().optional(),
|
|
291
302
|
action: z.enum(["webhook", "log"]).optional(), webhook_url: z.string().optional(),
|
|
292
303
|
}, (args) => ({ content: [{ type: "text", text: JSON.stringify(createAlertRule(db, { ...args, project_id: rp(args.project_id) ?? args.project_id })) }] }))
|
|
293
304
|
|
|
294
|
-
|
|
305
|
+
registerTool("list_alert_rules", {
|
|
295
306
|
project_id: z.string().optional(),
|
|
296
307
|
}, ({ project_id }) => ({
|
|
297
308
|
content: [{ type: "text", text: JSON.stringify(listAlertRules(db, rp(project_id))) }]
|
|
298
309
|
}))
|
|
299
310
|
|
|
300
|
-
|
|
311
|
+
registerTool("delete_alert_rule", { id: z.string() }, ({ id }) => {
|
|
301
312
|
deleteAlertRule(db, id)
|
|
302
313
|
return { content: [{ type: "text", text: "deleted" }] }
|
|
303
314
|
})
|
|
304
315
|
|
|
305
|
-
|
|
316
|
+
registerTool("get_health", {}, () => ({
|
|
306
317
|
content: [{ type: "text", text: JSON.stringify(getHealth(db)) }]
|
|
307
318
|
}))
|
|
308
319
|
|