mcp-osp-prompt 1.0.0

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/utils.js ADDED
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Utility functions for MCP server to reduce code duplication
3
+ */
4
+
5
+ /**
6
+ * Create a standard JSON-RPC success response
7
+ * @param {number|string} id - Request ID
8
+ * @param {any} result - Response result
9
+ * @returns {object} JSON-RPC response
10
+ */
11
+ export function createSuccessResponse(id, result) {
12
+ return {
13
+ jsonrpc: '2.0',
14
+ id,
15
+ result
16
+ };
17
+ }
18
+
19
+ /**
20
+ * Create a standard JSON-RPC error response
21
+ * @param {number|string} id - Request ID
22
+ * @param {number} code - Error code
23
+ * @param {string} message - Error message
24
+ * @returns {object} JSON-RPC error response
25
+ */
26
+ export function createErrorResponse(id, code, message) {
27
+ return {
28
+ jsonrpc: '2.0',
29
+ id,
30
+ error: { code, message }
31
+ };
32
+ }
33
+
34
+ /**
35
+ * Standard error codes for MCP
36
+ */
37
+ export const MCP_ERROR_CODES = {
38
+ PARSE_ERROR: -32700,
39
+ INVALID_REQUEST: -32600,
40
+ METHOD_NOT_FOUND: -32601,
41
+ INVALID_PARAMS: -32602,
42
+ INTERNAL_ERROR: -32603
43
+ };
44
+
45
+ /**
46
+ * Check if a request is a notification (no response expected)
47
+ * @param {object} req - JSON-RPC request
48
+ * @returns {boolean} True if notification
49
+ */
50
+ export function isNotification(req) {
51
+ return req.id === undefined || req.id === null;
52
+ }
53
+
54
+ /**
55
+ * Create standardized tool description for MCP tools list
56
+ * @param {string} name - Tool name
57
+ * @param {string} description - Tool description
58
+ * @param {object} inputSchema - JSON schema for input
59
+ * @returns {object} Tool description
60
+ */
61
+ export function createToolDescription(name, description, inputSchema) {
62
+ return {
63
+ name,
64
+ description,
65
+ inputSchema
66
+ };
67
+ }
68
+
69
+ /**
70
+ * Create standardized prompt description for MCP prompts list
71
+ * @param {string} name - Prompt name
72
+ * @param {string} description - Prompt description
73
+ * @param {Array} arguments - Prompt arguments
74
+ * @returns {object} Prompt description
75
+ */
76
+ export function createPromptDescription(name, description, arguments_) {
77
+ return {
78
+ name,
79
+ description,
80
+ arguments: arguments_
81
+ };
82
+ }
83
+
84
+ /**
85
+ * Validate JSON-RPC request structure
86
+ * @param {object} req - Request to validate
87
+ * @returns {object} Validation result
88
+ */
89
+ export function validateRequest(req) {
90
+ const errors = [];
91
+
92
+ if (!req || typeof req !== 'object') {
93
+ errors.push('Request must be an object');
94
+ }
95
+
96
+ if (!req.jsonrpc || req.jsonrpc !== '2.0') {
97
+ errors.push('Invalid or missing jsonrpc version');
98
+ }
99
+
100
+ if (!req.method || typeof req.method !== 'string') {
101
+ errors.push('Invalid or missing method');
102
+ }
103
+
104
+ return {
105
+ isValid: errors.length === 0,
106
+ errors
107
+ };
108
+ }
109
+
110
+ /**
111
+ * Wrap async handler with error handling
112
+ * @param {Function} handler - Async handler function
113
+ * @returns {Function} Wrapped handler
114
+ */
115
+ export function withErrorHandling(handler) {
116
+ return async (req) => {
117
+ try {
118
+ const validation = validateRequest(req);
119
+ if (!validation.isValid) {
120
+ return createErrorResponse(req.id, MCP_ERROR_CODES.INVALID_REQUEST,
121
+ `Invalid request: ${validation.errors.join(', ')}`);
122
+ }
123
+
124
+ return await handler(req);
125
+ } catch (error) {
126
+ console.error(`Handler error for ${req.method}:`, error);
127
+ return createErrorResponse(req.id, MCP_ERROR_CODES.INTERNAL_ERROR, error.message);
128
+ }
129
+ };
130
+ }
131
+
132
+ /**
133
+ * Standardized MCP server info
134
+ */
135
+ export const SERVER_INFO = {
136
+ name: 'osp-prompt',
137
+ version: '1.0.0'
138
+ };
139
+
140
+ /**
141
+ * Standardized MCP capabilities
142
+ */
143
+ export const SERVER_CAPABILITIES = {
144
+ tools: {}
145
+ };