-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmcp-server-lib-metrics.el
More file actions
121 lines (93 loc) · 3.88 KB
/
mcp-server-lib-metrics.el
File metadata and controls
121 lines (93 loc) · 3.88 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
;;; mcp-server-lib-metrics.el --- Metrics collection for MCP server -*- lexical-binding: t; -*-
;; Copyright (C) 2025 Laurynas Biveinis
;; Version: 0.2.0
;; Homepage: https://github.com/laurynas-biveinis/mcp.el
;; This file is part of mcp-server-lib.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file provides metrics collection functionality for the MCP server.
;; It tracks usage statistics for all MCP operations including tools,
;; resources, and prompts.
;;; Code:
(require 'cl-lib)
;;; Data structures
(cl-defstruct
mcp-server-lib-metrics
"Metrics for any MCP operation.
This structure tracks usage statistics for MCP operations including
method calls (e.g., \"initialize\", \"tools/list\") and tool-specific
calls (e.g., \"tools/call:my-tool\").
Slots:
`calls' - Total number of times the operation was invoked
`errors' - Number of times the operation resulted in an error"
(calls 0 :type integer :documentation "Total number of invocations.")
(errors
0
:type integer
:documentation "Number of failed invocations."))
;;; Global state
(defvar mcp-server-lib-metrics--table (make-hash-table :test 'equal)
"Metrics for all MCP operations. Key: method or method:name.")
;;; Core functions
(defun mcp-server-lib-metrics--get (key)
"Get metrics for KEY, creating if needed."
(or (gethash key mcp-server-lib-metrics--table)
(puthash
key
(make-mcp-server-lib-metrics)
mcp-server-lib-metrics--table)))
(defun mcp-server-lib-metrics--error-rate (calls errors)
"Calculate error rate percentage.
Compute the percentage of ERRORS relative to total CALLS.
Return a float between 0.0 and 100.0."
(if (zerop calls)
0.0
(* 100.0 (/ (float errors) calls))))
(defun mcp-server-lib-metrics--track-tool-call
(tool-name &optional is-error)
"Track a tool call for TOOL-NAME.
If IS-ERROR is non-nil, also increment the error counter."
(let ((metrics
(mcp-server-lib-metrics--get
(format "tools/call:%s" tool-name))))
(cl-incf (mcp-server-lib-metrics-calls metrics))
(when is-error
(cl-incf (mcp-server-lib-metrics-errors metrics)))))
;;; Public functions
(defun mcp-server-lib-metrics-get (operation)
"Get metrics object for a specific OPERATION.
Return a metrics object with zero values if OPERATION has not been tracked.
Arguments:
OPERATION - The operation name (e.g., \"tools/list\",
\"tools/call:my-tool\")
Use `mcp-server-lib-metrics-calls' and `mcp-server-lib-metrics-errors'
to access the metrics values from the returned object."
(or (gethash operation mcp-server-lib-metrics--table)
(make-mcp-server-lib-metrics)))
(defun mcp-server-lib-metrics-summary ()
"Return metrics summary as a string."
(let ((total-calls 0)
(total-errors 0))
(maphash
(lambda (_key metrics)
(cl-incf total-calls (mcp-server-lib-metrics-calls metrics))
(cl-incf total-errors (mcp-server-lib-metrics-errors metrics)))
mcp-server-lib-metrics--table)
(format "MCP metrics: %d calls, %d errors (%.1f%% error rate)"
total-calls total-errors
(mcp-server-lib-metrics--error-rate
total-calls total-errors))))
(provide 'mcp-server-lib-metrics)
;; Local Variables:
;; package-lint-main-file: "mcp-server-lib.el"
;; End:
;;; mcp-server-lib-metrics.el ends here