UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines • 6.54 kB
import{expectObjectRecord,expectOnlyKnownKeys}from"#internal/authored-module.js";import{parseJsonValue}from"#shared/json.js";import{normalizeApproval}from"#internal/authored-definition/approval.js";import{normalizeAuthorizationSpec}from"#shared/validate-authorization.js";const KNOWN_TOP_LEVEL_KEYS=[`approval`,`auth`,`description`,`headers`,`instanceKey`,`protocolVersionDiscovery`,`toolCall`,`tools`,`url`],KNOWN_OPENAPI_TOP_LEVEL_KEYS=[`approval`,`auth`,`baseUrl`,`description`,`headers`,`instanceKey`,`operations`,`spec`,`toolCall`],KNOWN_AUTHORIZATION_KEYS=[`completeAuthorization`,`credentialOwner`,`evict`,`getToken`,`principalType`,`startAuthorization`,`vercelConnect`],KNOWN_TOOL_CALL_KEYS=[`providedArguments`];function normalizeMcpClientConnectionDefinition(e,t){let n=expectObjectRecord(e,t);expectOnlyKnownKeys(n,KNOWN_TOP_LEVEL_KEYS,t),validateUrl(n,t),validateDescription(n,t),validateInstanceKey(n,t);let r=normalizeAuthorization(n,t),i=normalizeHeaders(n,t),a=normalizeConnectionToolCall(n,t),o=normalizeToolFilter(n,t);if(r!==void 0&&i!==void 0&&typeof i!=`function`&&Object.keys(i).some(e=>e.toLowerCase()===`authorization`))throw Error(`${t} "headers" must not include an "Authorization" key when "auth" is also provided.`);let s={description:n.description,url:n.url};if(n.protocolVersionDiscovery!==void 0){if(typeof n.protocolVersionDiscovery!=`boolean`)throw Error(`${t} "protocolVersionDiscovery" must be a boolean.`);s.protocolVersionDiscovery=n.protocolVersionDiscovery}return n.instanceKey!==void 0&&(s.instanceKey=n.instanceKey),r!==void 0&&(s.auth=r),i!==void 0&&(s.headers=i),a!==void 0&&(s.toolCall=a),o!==void 0&&(s.tools=o),n.approval!==void 0&&(s.approval=normalizeApproval(n.approval,t)),s}function normalizeConnectionToolCall(e,t){if(e.toolCall===void 0)return;let n=expectObjectRecord(e.toolCall,`${t} The "toolCall" field must be a plain object.`);if(expectOnlyKnownKeys(n,KNOWN_TOOL_CALL_KEYS,`${t} The "toolCall" field`),n.providedArguments===void 0)return{};let r=expectObjectRecord(n.providedArguments,`${t} The "toolCall.providedArguments" field must be a plain object.`);for(let[e,n]of Object.entries(r))if(typeof n!=`function`&&!(typeof n==`object`&&n&&typeof n.then==`function`))try{parseJsonValue(n)}catch{throw Error(`${t} The "toolCall.providedArguments.${e}" value must be JSON-serializable, a Promise, or a function.`)}return{providedArguments:r}}function normalizeOpenApiConnectionDefinition(e,t){let n=expectObjectRecord(e,t);expectOnlyKnownKeys(n,KNOWN_OPENAPI_TOP_LEVEL_KEYS,t),validateSpec(n,t),validateBaseUrl(n,t),validateDescription(n,t),validateInstanceKey(n,t);let r=normalizeAuthorization(n,t),i=normalizeHeaders(n,t),a=normalizeFilterField(n,`operations`,t),o=normalizeConnectionToolCall(n,t);if(r!==void 0&&i!==void 0&&typeof i!=`function`&&Object.keys(i).some(e=>e.toLowerCase()===`authorization`))throw Error(`${t} "headers" must not include an "Authorization" key when "auth" is also provided.`);let s={description:n.description,spec:n.spec};return n.baseUrl!==void 0&&(s.baseUrl=n.baseUrl),n.instanceKey!==void 0&&(s.instanceKey=n.instanceKey),r!==void 0&&(s.auth=r),i!==void 0&&(s.headers=i),o!==void 0&&(s.toolCall=o),a!==void 0&&(s.operations=a),n.approval!==void 0&&(s.approval=normalizeApproval(n.approval,t)),s}function validateSpec(e,t){let n=e.spec;if(typeof n==`string`){if(!URL.canParse(n))throw Error(`${t} The "spec" field must be a valid URL when provided as a string.`);let e=new URL(n);if(e.protocol!==`https:`&&e.protocol!==`http:`)throw Error(`${t} The "spec" URL must use the http or https protocol, got "${e.protocol}".`);return}if(typeof n!=`object`||!n||Array.isArray(n))throw Error(`${t} The "spec" field must be a URL string or an inline OpenAPI document object.`)}function validateBaseUrl(e,t){if(e.baseUrl===void 0)return;if(typeof e.baseUrl!=`string`||!URL.canParse(e.baseUrl))throw Error(`${t} The "baseUrl" field must be a valid URL when provided.`);let n=new URL(e.baseUrl);if(n.protocol!==`https:`&&n.protocol!==`http:`)throw Error(`${t} The "baseUrl" field must use the http or https protocol, got "${n.protocol}".`)}function validateUrl(e,t){if(typeof e.url!=`string`||!URL.canParse(e.url))throw Error(`${t} The "url" field must be a valid URL.`);let n=new URL(e.url);if(n.protocol!==`https:`&&n.protocol!==`http:`)throw Error(`${t} The "url" field must use the http or https protocol, got "${n.protocol}".`)}function validateDescription(e,t){if(typeof e.description!=`string`||e.description.length===0)throw Error(`${t} The "description" field must be a non-empty string.`)}function validateInstanceKey(e,t){if(e.instanceKey!==void 0&&(typeof e.instanceKey!=`string`||e.instanceKey.length===0))throw Error(`${t} The "instanceKey" field must be a non-empty string.`)}function normalizeAuthorization(e,t){if(e.auth===void 0)return;if(typeof e.auth==`function`)return e.auth;let n=expectObjectRecord(e.auth,`${t} The "auth" field must be an object with a "getToken" method or a function.`);return expectOnlyKnownKeys(n,KNOWN_AUTHORIZATION_KEYS,`${t} The "auth" field`),normalizeAuthorizationSpec(n,t)}function normalizeHeaders(e,t){if(e.headers===void 0)return;if(typeof e.headers==`function`)return e.headers;if(typeof e.headers!=`object`||e.headers===null||Array.isArray(e.headers))throw Error(`${t} The "headers" field must be a plain object or a function.`);let n=e.headers;for(let[e,r]of Object.entries(n)){let n=typeof r;if(n!==`string`&&n!==`function`&&n!==`object`||n===`object`&&(r===null||typeof r.then!=`function`))throw Error(`${t} The "headers.${e}" value must be a string, Promise, or function.`)}return n}function normalizeToolFilter(e,t){return normalizeFilterField(e,`tools`,t)}function normalizeFilterField(e,t,n){let r=e[t];if(r===void 0)return;if(typeof r!=`object`||!r||Array.isArray(r))throw Error(`${n} The "${t}" field must specify either "allow" or "block".`);let i=r,a=`allow`in i,o=`block`in i;if(a&&o)throw Error(`${n} The "${t}" field must specify either "allow" or "block", not both.`);if(!a&&!o)throw Error(`${n} The "${t}" field must specify either "allow" or "block".`);return a?(validateStringArray(i.allow,`${n} The "${t}.allow"`),{allow:i.allow}):(validateStringArray(i.block,`${n} The "${t}.block"`),{block:i.block})}function validateStringArray(e,t){if(!Array.isArray(e))throw Error(`${t} field must be an array of strings.`);for(let n=0;n<e.length;n++)if(typeof e[n]!=`string`)throw Error(`${t}[${n}] must be a string.`)}export{normalizeMcpClientConnectionDefinition,normalizeOpenApiConnectionDefinition};