"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _indexjs = require('@modelcontextprotocol/sdk/server/index.js');var _typesjs = require('@modelcontextprotocol/sdk/types.js');var _v4 = require('zod/v4');function g(e){try{return new URL(e),e}catch (e2){throw new Error(`invalid uri: ${e}`)}}function T(e,r){let o=new URL(e),a=new URL(r);return o.href===a.href}function h(e,r){let a=new URL(e).pathname.split("/").slice(1);for(let m of r){let s=new URL(m),t=decodeURIComponent(s.pathname).split("/").slice(1);if(a.length!==t.length)continue;let n={},l=!0;for(let i=0;i{if("uri"in o){let s=new URL(o.uri,`${e}://`),t=decodeURI(s.href);return{...o,uri:t}}let a=new URL(o.uriTemplate,`${e}://`),m=decodeURI(a.href);return{...o,uriTemplate:m}})}function J(e,r){return{uri:e,mimeType:"application/json",text:JSON.stringify(r)}}function A(e){return e}function W(e){let r={};e.resources&&(r.resources={}),e.tools&&(r.tools={});let o=new (0, _indexjs.Server)({name:e.name,title:e.title,version:e.version},{capabilities:r,instructions:e.instructions});async function a(){if(!e.resources)throw new Error("resources not available");return typeof e.resources=="function"?await e.resources():e.resources}async function m(){if(!e.tools)throw new Error("tools not available");return typeof e.tools=="function"?await e.tools():e.tools}return o.oninitialized=async()=>{let s=o.getClientVersion(),t=o.getClientCapabilities();if(!s)throw new Error("client info not available after initialization");if(!t)throw new Error("client capabilities not available after initialization");let n={clientInfo:s,clientCapabilities:t};await _optionalChain([e, 'access', _ => _.onInitialize, 'optionalCall', _2 => _2(n)])},e.resources&&(o.setRequestHandler(_typesjs.ListResourcesRequestSchema,async()=>({resources:(await a()).filter(t=>"uri"in t).map(({uri:t,name:n,description:l,mimeType:i})=>({uri:t,name:n,description:l,mimeType:i}))})),o.setRequestHandler(_typesjs.ListResourceTemplatesRequestSchema,async()=>({resourceTemplates:(await a()).filter(t=>"uriTemplate"in t).map(({uriTemplate:t,name:n,description:l,mimeType:i})=>({uriTemplate:t,name:n,description:l,mimeType:i}))})),o.setRequestHandler(_typesjs.ReadResourceRequestSchema,async s=>{try{let t=await a(),{uri:n}=s.params,i=t.filter(c=>"uri"in c).find(c=>T(c.uri,n));if(i){let c=await i.read(n);return{contents:Array.isArray(c)?c:[c]}}let u=t.filter(c=>"uriTemplate"in c),R=u.map(({uriTemplate:c})=>g(c)),f=h(n,R);if(!f)throw new Error("resource not found");let y=u.find(c=>c.uriTemplate===f.uri);if(!y)throw new Error("resource not found");let p=await y.read(n,f.params);return{contents:Array.isArray(p)?p:[p]}}catch(t){return{isError:!0,content:[{type:"text",text:JSON.stringify({error:x(t)})}]}}})),e.tools&&(o.setRequestHandler(_typesjs.ListToolsRequestSchema,async()=>{let s=await m();return{tools:await Promise.all(Object.entries(s).map(async([t,{description:n,annotations:l,parameters:i}])=>{let u=_v4.z.toJSONSchema(i,{target:"draft-7"});return{name:t,description:typeof n=="function"?await n():n,annotations:l,inputSchema:u}}))}}),o.setRequestHandler(_typesjs.CallToolRequestSchema,async s=>{try{let t=await m(),n=s.params.name;if(!(n in t))throw new Error("tool not found");let l=t[n];if(!l)throw new Error("tool not found");let i=l.parameters.strict().parse(_nullishCoalesce(s.params.arguments, () => ({}))),R=await(async y=>{let p=await y.execute(i).then(d=>({success:!0,data:d})).catch(d=>({success:!1,error:d}));try{_optionalChain([e, 'access', _3 => _3.onToolCall, 'optionalCall', _4 => _4({name:n,arguments:i,annotations:y.annotations,...p})])}catch(d){console.error("Failed to run tool callback",d)}if(!p.success)throw p.error;return p.data})(l);return{content:R!=null?[{type:"text",text:JSON.stringify(R)}]:[]}}catch(t){return{isError:!0,content:[{type:"text",text:JSON.stringify({error:x(t)})}]}}})),o}function x(e){if(!e||typeof e!="object")return e;let r={},o=["name","message"];for(let a of o)a in e&&(r[a]=e[a]);return r}var w=class{#e;#t;constructor(){let r,o,a=new Promise(s=>{r=s}),m=new Promise(s=>{o=s});this.ready=Promise.all([a,m]).then(()=>{}),this.readable=new ReadableStream({start:s=>{this.#e=s,r()}}),this.writable=new WritableStream({start:s=>{this.#t=s,o()},write:s=>{_optionalChain([this, 'access', _5 => _5.onmessage, 'optionalCall', _6 => _6(s)])}})}async start(){await this.ready}async send(r){if(!this.#e)throw new Error("readable stream not initialized");this.#e.enqueue(r)}async close(){_optionalChain([this, 'access', _7 => _7.#e, 'optionalAccess', _8 => _8.error, 'call', _9 => _9(new Error("connection closed"))]),_optionalChain([this, 'access', _10 => _10.#t, 'optionalAccess', _11 => _11.error, 'call', _12 => _12(new Error("connection closed"))]),_optionalChain([this, 'access', _13 => _13.onclose, 'optionalCall', _14 => _14()])}};exports.StreamTransport = w; exports.createMcpServer = W; exports.jsonResource = q; exports.jsonResourceResponse = J; exports.jsonResourceTemplate = I; exports.resource = L; exports.resourceTemplate = z; exports.resources = M; exports.tool = A; //# sourceMappingURL=index.cjs.map