@1mcp/agent 0.2.0 → 0.7.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.
@@ -1,47 +1,11 @@
1
1
  import { ERROR_CODES } from '../constants.js';
2
2
  import logger from '../logger/logger.js';
3
+ import { MCPError, InvalidRequestError } from './errorTypes.js';
3
4
  /**
4
- * Custom error types for the MCP agent
5
- */
6
- export class MCPError extends Error {
7
- code;
8
- data;
9
- constructor(message, code, data) {
10
- super(message);
11
- this.name = 'MCPError';
12
- this.code = code;
13
- this.data = data;
14
- }
15
- }
16
- export class ClientNotFoundError extends MCPError {
17
- constructor(clientName) {
18
- super(`Client not found: ${clientName}`, ERROR_CODES.TRANSPORT_NOT_FOUND);
19
- this.name = 'ClientNotFoundError';
20
- }
21
- }
22
- export class ClientConnectionError extends MCPError {
23
- constructor(clientName, originalError) {
24
- super(`Failed to connect to client: ${clientName}${originalError ? ` - ${originalError.message}` : ''}`, ERROR_CODES.TRANSPORT_NOT_FOUND, { originalError });
25
- this.name = 'ClientConnectionError';
26
- }
27
- }
28
- export class InvalidRequestError extends MCPError {
29
- constructor(message, data) {
30
- super(message, ERROR_CODES.INVALID_PARAMS, data);
31
- this.name = 'InvalidRequestError';
32
- }
33
- }
34
- export class ProxyError extends MCPError {
35
- constructor(message, originalError) {
36
- super(message, ERROR_CODES.INTERNAL_SERVER_ERROR, originalError ? { originalError: originalError.message } : undefined);
37
- this.name = 'ProxyError';
38
- }
39
- }
40
- /**
41
- * Handles errors in async functions by wrapping them in a try/catch block
42
- * @param fn The async function to wrap
43
- * @param errorMessage The error message to log
44
- * @returns A wrapped function that handles errors
5
+ * Wraps a function with error handling
6
+ * @param fn The function to wrap
7
+ * @param errorMessage The error message to use if the function fails
8
+ * @returns The wrapped function
45
9
  */
46
10
  export function withErrorHandling(fn, errorMessage) {
47
11
  return async (...args) => {
@@ -54,11 +18,70 @@ export function withErrorHandling(fn, errorMessage) {
54
18
  if (error instanceof MCPError) {
55
19
  throw error;
56
20
  }
57
- // Convert other errors to ProxyError
58
- throw new ProxyError(errorMessage, error instanceof Error ? error : new Error(String(error)));
21
+ // Convert other errors to MCPError
22
+ throw new MCPError(errorMessage, ERROR_CODES.INTERNAL_SERVER_ERROR, {
23
+ originalError: error instanceof Error ? error : new Error(String(error)),
24
+ });
59
25
  }
60
26
  };
61
27
  }
28
+ /**
29
+ * Normalizes an error to an MCPError type
30
+ * @param error The error to normalize
31
+ * @param errorMessage The error message to use if the error is not an MCPError
32
+ * @returns The normalized error
33
+ */
34
+ export function normalizeError(error, errorMessage) {
35
+ if (error instanceof MCPError) {
36
+ return error;
37
+ }
38
+ if (error instanceof Error) {
39
+ return new MCPError(error.message || errorMessage, ERROR_CODES.INTERNAL_SERVER_ERROR);
40
+ }
41
+ return new MCPError(errorMessage, ERROR_CODES.INTERNAL_SERVER_ERROR);
42
+ }
43
+ /**
44
+ * Checks if an error is a specific MCPError type
45
+ * @param error The error to check
46
+ * @param errorType The error type to check against
47
+ * @returns True if the error is of the specified type
48
+ */
49
+ export function isMCPError(error, errorType) {
50
+ return error instanceof errorType;
51
+ }
52
+ /**
53
+ * Gets the error code from an error
54
+ * @param error The error to get the code from
55
+ * @returns The error code
56
+ */
57
+ export function getErrorCode(error) {
58
+ if (error instanceof MCPError) {
59
+ return error.code;
60
+ }
61
+ return ERROR_CODES.INTERNAL_SERVER_ERROR;
62
+ }
63
+ /**
64
+ * Gets the error message from an error
65
+ * @param error The error to get the message from
66
+ * @returns The error message
67
+ */
68
+ export function getErrorMessage(error) {
69
+ if (error instanceof Error) {
70
+ return error.message;
71
+ }
72
+ return String(error);
73
+ }
74
+ /**
75
+ * Gets the error cause from an error
76
+ * @param error The error to get the cause from
77
+ * @returns The error cause
78
+ */
79
+ export function getErrorCause(error) {
80
+ if (error instanceof Error && 'cause' in error) {
81
+ return error.cause;
82
+ }
83
+ return undefined;
84
+ }
62
85
  /**
63
86
  * Formats an error for JSON-RPC response
64
87
  * @param error The error to format
@@ -0,0 +1,47 @@
1
+ import { ERROR_CODES } from '../constants.js';
2
+ export class MCPError extends Error {
3
+ code;
4
+ data;
5
+ constructor(message, code, data) {
6
+ super(message);
7
+ this.name = 'MCPError';
8
+ this.code = code;
9
+ this.data = data;
10
+ }
11
+ }
12
+ export class ClientConnectionError extends MCPError {
13
+ constructor(clientName, cause) {
14
+ super(`Failed to connect to client ${clientName}: ${cause.message}`, ERROR_CODES.TRANSPORT_NOT_FOUND, { cause });
15
+ this.name = 'ClientConnectionError';
16
+ }
17
+ }
18
+ export class ClientNotFoundError extends MCPError {
19
+ constructor(clientName) {
20
+ super(`Client not found: ${clientName}`, ERROR_CODES.TRANSPORT_NOT_FOUND);
21
+ this.name = 'ClientNotFoundError';
22
+ }
23
+ }
24
+ export class ClientOperationError extends MCPError {
25
+ constructor(clientName, operation, cause, context) {
26
+ super(`Operation ${operation} failed on client ${clientName}: ${cause.message}`, ERROR_CODES.INTERNAL_SERVER_ERROR, { cause, context });
27
+ this.name = 'ClientOperationError';
28
+ }
29
+ }
30
+ export class ValidationError extends MCPError {
31
+ constructor(message, validationErrors) {
32
+ super(message, ERROR_CODES.INVALID_PARAMS, { validationErrors });
33
+ this.name = 'ValidationError';
34
+ }
35
+ }
36
+ export class TransportError extends MCPError {
37
+ constructor(transportName, cause) {
38
+ super(`Transport error for ${transportName}: ${cause.message}`, ERROR_CODES.CLIENT_CONNECTION_ERROR, { cause });
39
+ this.name = 'TransportError';
40
+ }
41
+ }
42
+ export class InvalidRequestError extends MCPError {
43
+ constructor(message, data) {
44
+ super(message, ERROR_CODES.INVALID_PARAMS, data);
45
+ this.name = 'InvalidRequestError';
46
+ }
47
+ }
package/package.json CHANGED
@@ -1,8 +1,26 @@
1
1
  {
2
2
  "name": "@1mcp/agent",
3
- "version": "0.2.0",
3
+ "version": "0.7.0",
4
4
  "description": "One MCP server to aggregate them all - A unified Model Context Protocol server implementation",
5
- "homepage": "https://1mcp.app",
5
+ "keywords": [
6
+ "mcp",
7
+ "mcp-server",
8
+ "mcp-client",
9
+ "model-context-protocol",
10
+ "agent",
11
+ "proxy",
12
+ "server",
13
+ "cli",
14
+ "ai",
15
+ "llm",
16
+ "model-aggregation",
17
+ "typescript"
18
+ ],
19
+ "author": "William Xu <xuzp@1mcp.app>",
20
+ "homepage": "https://github.com/1mcp-app/agent",
21
+ "bugs": {
22
+ "url": "https://github.com/1mcp-app/agent/issues"
23
+ },
6
24
  "license": "Apache-2.0",
7
25
  "repository": {
8
26
  "type": "git",
@@ -25,6 +43,7 @@
25
43
  },
26
44
  "lint-staged": {
27
45
  "*.{js,ts}": [
46
+ "eslint --fix",
28
47
  "prettier --write"
29
48
  ]
30
49
  },
@@ -39,20 +58,38 @@
39
58
  "zod": "^3.24.2"
40
59
  },
41
60
  "devDependencies": {
61
+ "@eslint/js": "^9.22.0",
62
+ "@testing-library/jest-dom": "^6.6.3",
42
63
  "@types/express": "^5.0.0",
64
+ "@types/jest": "^29.5.14",
43
65
  "@types/node": "^22.13.10",
44
66
  "@types/uuid": "^10.0.0",
45
67
  "@types/yargs": "^17.0.33",
68
+ "@typescript-eslint/eslint-plugin": "^8.27.0",
69
+ "@typescript-eslint/parser": "^8.27.0",
70
+ "eslint": "^9.22.0",
71
+ "eslint-config-prettier": "^10.1.1",
72
+ "eslint-plugin-prettier": "^5.2.3",
73
+ "globals": "^16.0.0",
46
74
  "husky": "^9.1.7",
75
+ "jest": "^29.7.0",
76
+ "jiti": "^2.4.2",
47
77
  "lint-staged": "^15.5.0",
48
78
  "prettier": "^3.5.3",
79
+ "ts-jest": "^29.2.6",
80
+ "ts-node": "^10.9.2",
49
81
  "tsc-watch": "^6.2.1",
50
82
  "typescript": "^5.3.3"
51
83
  },
52
84
  "scripts": {
53
- "dev": "tsc-watch --onSuccess \"node build/index.js\"",
85
+ "dev": "tsc-watch --onSuccess \"node build/index.js --config ./mcp.json\"",
54
86
  "build": "tsc && node -e \"require('fs').chmodSync('build/index.js', '755')\"",
55
87
  "watch": "tsc --watch",
56
- "inspector": "npx @modelcontextprotocol/inspector build/index.js"
88
+ "inspector": "npx -y @modelcontextprotocol/inspector",
89
+ "lint": "eslint . --ext .ts",
90
+ "lint:fix": "eslint . --ext .ts --fix",
91
+ "test": "jest",
92
+ "test:watch": "jest --watch",
93
+ "test:coverage": "jest --coverage"
57
94
  }
58
95
  }