@codebolt/codeboltjs 2.0.2 → 2.0.4
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/Readme.md +47 -0
- package/{modules → dist}/agentlib/agent.js +63 -54
- package/{modules → dist}/agentlib/usermessage.js +3 -3
- package/{index.d.ts → dist/index.d.ts} +3 -0
- package/{index.js → dist/index.js} +2 -0
- package/dist/utils/editFile.d.ts +4 -0
- package/dist/utils/editFile.js +30 -0
- package/dist/utils.d.ts +5 -0
- package/{utils.js → dist/utils.js} +4 -4
- package/package.json +17 -8
- package/.codebolt/chat.json +0 -1
- package/.codebolt/debug.json +0 -1
- package/.codebolt/projectState.json +0 -127
- package/.codebolt/tasks.json +0 -1
- package/.codeboltconfig.yaml +0 -0
- package/.github/workflows/publish-to-npm.yml +0 -20
- package/bkp/toolBox.bkp.ts +0 -1162
- package/docs/.nojekyll +0 -1
- package/docs/assets/highlight.css +0 -127
- package/docs/assets/icons.js +0 -15
- package/docs/assets/icons.svg +0 -1
- package/docs/assets/main.js +0 -59
- package/docs/assets/navigation.js +0 -1
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1412
- package/docs/classes/_internal_.Agent.html +0 -318
- package/docs/classes/_internal_.AsyncResource.html +0 -44
- package/docs/classes/_internal_.Blob.html +0 -33
- package/docs/classes/_internal_.ClientRequest.html +0 -506
- package/docs/classes/_internal_.Codebolt.html +0 -315
- package/docs/classes/_internal_.CustomEventEmitter-1.html +0 -248
- package/docs/classes/_internal_.CustomEventEmitter.html +0 -248
- package/docs/classes/_internal_.Duplex.html +0 -690
- package/docs/classes/_internal_.EventEmitter-1.html +0 -254
- package/docs/classes/_internal_.EventEmitter.EventEmitterAsyncResource.html +0 -268
- package/docs/classes/_internal_.IncomingMessage.html +0 -661
- package/docs/classes/_internal_.KeyObject.html +0 -76
- package/docs/classes/_internal_.OutgoingMessage.html +0 -491
- package/docs/classes/_internal_.Readable.html +0 -551
- package/docs/classes/_internal_.ReadableBase.html +0 -551
- package/docs/classes/_internal_.Server-1.html +0 -416
- package/docs/classes/_internal_.Server-2.html +0 -326
- package/docs/classes/_internal_.Server-3.html +0 -353
- package/docs/classes/_internal_.Server.html +0 -390
- package/docs/classes/_internal_.ServerResponse.html +0 -569
- package/docs/classes/_internal_.Socket.html +0 -791
- package/docs/classes/_internal_.Stream.html +0 -256
- package/docs/classes/_internal_.TLSSocket.html +0 -947
- package/docs/classes/_internal_.URL.html +0 -140
- package/docs/classes/_internal_.URLSearchParams.html +0 -68
- package/docs/classes/_internal_.WebSocket-1.html +0 -297
- package/docs/classes/_internal_.WebSocket.Server.html +0 -261
- package/docs/classes/_internal_.Writable.html +0 -394
- package/docs/classes/_internal_.WritableBase.html +0 -394
- package/docs/classes/_internal_.X509Certificate.html +0 -144
- package/docs/classes/_internal_.internal-1.html +0 -256
- package/docs/classes/_internal_.internal.PassThrough.html +0 -686
- package/docs/classes/_internal_.internal.Transform.html +0 -693
- package/docs/enums/_internal_.logType.html +0 -4
- package/docs/functions/_internal_.WebSocket.createWebSocketStream.html +0 -1
- package/docs/functions/_internal_._node_stream_consumers_.arrayBuffer.html +0 -1
- package/docs/functions/_internal_._node_stream_consumers_.blob.html +0 -1
- package/docs/functions/_internal_._node_stream_consumers_.buffer.html +0 -1
- package/docs/functions/_internal_._node_stream_consumers_.json.html +0 -1
- package/docs/functions/_internal_._node_stream_consumers_.text.html +0 -1
- package/docs/functions/_internal_._node_stream_promises_.finished.html +0 -1
- package/docs/functions/_internal_._node_stream_promises_.pipeline.html +0 -1
- package/docs/functions/_internal_.internal.addAbortSignal.html +0 -17
- package/docs/functions/_internal_.internal.finished-1.html +0 -20
- package/docs/functions/_internal_.internal.finished.__promisify__.html +0 -1
- package/docs/functions/_internal_.internal.getDefaultHighWaterMark.html +0 -4
- package/docs/functions/_internal_.internal.isErrored.html +0 -3
- package/docs/functions/_internal_.internal.isReadable.html +0 -3
- package/docs/functions/_internal_.internal.pipeline-1.html +0 -24
- package/docs/functions/_internal_.internal.pipeline.__promisify__.html +0 -1
- package/docs/functions/_internal_.internal.setDefaultHighWaterMark.html +0 -4
- package/docs/hierarchy.html +0 -1
- package/docs/index.html +0 -12
- package/docs/interfaces/_internal_.AddToAgentStateResponse.html +0 -3
- package/docs/interfaces/_internal_.AddTokenResponse.html +0 -3
- package/docs/interfaces/_internal_.AddVectorItemResponse.html +0 -3
- package/docs/interfaces/_internal_.AddressInfo.html +0 -4
- package/docs/interfaces/_internal_.AgentOptions.html +0 -35
- package/docs/interfaces/_internal_.ApplicationState.html +0 -4
- package/docs/interfaces/_internal_.ArrayBufferView.html +0 -7
- package/docs/interfaces/_internal_.ArrayLike.html +0 -2
- package/docs/interfaces/_internal_.ArrayOptions.html +0 -7
- package/docs/interfaces/_internal_.AsymmetricKeyDetails.html +0 -15
- package/docs/interfaces/_internal_.AsyncGenerator.html +0 -5
- package/docs/interfaces/_internal_.AsyncGeneratorFunction.html +0 -12
- package/docs/interfaces/_internal_.AsyncIterable.html +0 -2
- package/docs/interfaces/_internal_.AsyncIterableIterator.html +0 -5
- package/docs/interfaces/_internal_.AsyncIterator.html +0 -4
- package/docs/interfaces/_internal_.AsyncResourceOptions.html +0 -12
- package/docs/interfaces/_internal_.BlobOptions.html +0 -8
- package/docs/interfaces/_internal_.Buffer.html +0 -760
- package/docs/interfaces/_internal_.BufferConstructor.html +0 -165
- package/docs/interfaces/_internal_.Certificate.html +0 -13
- package/docs/interfaces/_internal_.ChatMessage.html +0 -3
- package/docs/interfaces/_internal_.Choice.html +0 -5
- package/docs/interfaces/_internal_.CipherNameAndProtocol.html +0 -7
- package/docs/interfaces/_internal_.ClientRequestArgs.html +0 -28
- package/docs/interfaces/_internal_.CommandError.html +0 -3
- package/docs/interfaces/_internal_.CommonConnectionOptions.html +0 -29
- package/docs/interfaces/_internal_.ConnectOpts.html +0 -5
- package/docs/interfaces/_internal_.CreateFileResponse.html +0 -6
- package/docs/interfaces/_internal_.CreateFolderResponse.html +0 -6
- package/docs/interfaces/_internal_.CryptoKey.html +0 -26
- package/docs/interfaces/_internal_.DebugAddLogResponse.html +0 -4
- package/docs/interfaces/_internal_.DeleteFileResponse.html +0 -6
- package/docs/interfaces/_internal_.DeleteFolderResponse.html +0 -6
- package/docs/interfaces/_internal_.DetailedPeerCertificate.html +0 -53
- package/docs/interfaces/_internal_.Dict.html +0 -1
- package/docs/interfaces/_internal_.Disposable.html +0 -2
- package/docs/interfaces/_internal_.DropArgument.html +0 -7
- package/docs/interfaces/_internal_.DuplexOptions.html +0 -22
- package/docs/interfaces/_internal_.EphemeralKeyInfo.html +0 -7
- package/docs/interfaces/_internal_.ErrnoException.html +0 -8
- package/docs/interfaces/_internal_.Error.html +0 -4
- package/docs/interfaces/_internal_.EventEmitter-2.html +0 -130
- package/docs/interfaces/_internal_.EventEmitter.Abortable.html +0 -3
- package/docs/interfaces/_internal_.EventEmitter.EventEmitterAsyncResourceOptions.html +0 -19
- package/docs/interfaces/_internal_.EventEmitter.EventEmitterReferencingAsyncResource.html +0 -32
- package/docs/interfaces/_internal_.EventEmitterOptions.html +0 -3
- package/docs/interfaces/_internal_.ExtractTextResponse.html +0 -3
- package/docs/interfaces/_internal_.GetAgentStateResponse.html +0 -3
- package/docs/interfaces/_internal_.GetContentResponse.html +0 -3
- package/docs/interfaces/_internal_.GetMarkdownResponse.html +0 -3
- package/docs/interfaces/_internal_.GetMatcherListTreeResponse.html +0 -3
- package/docs/interfaces/_internal_.GetProjectPathResponse.html +0 -5
- package/docs/interfaces/_internal_.GetTokenResponse.html +0 -3
- package/docs/interfaces/_internal_.GetVectorResponse.html +0 -3
- package/docs/interfaces/_internal_.GoToPageResponse.html +0 -3
- package/docs/interfaces/_internal_.HtmlReceived.html +0 -3
- package/docs/interfaces/_internal_.IncomingHttpHeaders.html +0 -64
- package/docs/interfaces/_internal_.InformationEvent.html +0 -8
- package/docs/interfaces/_internal_.IpcSocketConnectOpts.html +0 -6
- package/docs/interfaces/_internal_.Iterable.html +0 -2
- package/docs/interfaces/_internal_.IterableIterator.html +0 -5
- package/docs/interfaces/_internal_.Iterator.html +0 -4
- package/docs/interfaces/_internal_.IteratorReturnResult.html +0 -3
- package/docs/interfaces/_internal_.IteratorYieldResult.html +0 -3
- package/docs/interfaces/_internal_.JsonWebKey.html +0 -14
- package/docs/interfaces/_internal_.JwkKeyExportOptions.html +0 -2
- package/docs/interfaces/_internal_.KeyAlgorithm.html +0 -2
- package/docs/interfaces/_internal_.KeyExportOptions.html +0 -5
- package/docs/interfaces/_internal_.KeyObject-1.html +0 -5
- package/docs/interfaces/_internal_.LLMResponse.html +0 -4
- package/docs/interfaces/_internal_.ListenOptions.html +0 -13
- package/docs/interfaces/_internal_.LookupAddress.html +0 -6
- package/docs/interfaces/_internal_.LookupOptions.html +0 -27
- package/docs/interfaces/_internal_.MatchProblemResponse.html +0 -3
- package/docs/interfaces/_internal_.MemoryGetResponse.html +0 -5
- package/docs/interfaces/_internal_.MemorySetResponse.html +0 -5
- package/docs/interfaces/_internal_.Message.html +0 -8
- package/docs/interfaces/_internal_.MessageContent.html +0 -3
- package/docs/interfaces/_internal_.Object.html +0 -18
- package/docs/interfaces/_internal_.OnReadOpts.html +0 -6
- package/docs/interfaces/_internal_.OpenDebugBrowserResponse.html +0 -4
- package/docs/interfaces/_internal_.OutgoingHttpHeaders.html +0 -80
- package/docs/interfaces/_internal_.PeerCertificate.html +0 -50
- package/docs/interfaces/_internal_.PromiseLike.html +0 -6
- package/docs/interfaces/_internal_.PxfObject.html +0 -5
- package/docs/interfaces/_internal_.QueryVectorItemResponse.html +0 -3
- package/docs/interfaces/_internal_.QueuingStrategy.html +0 -3
- package/docs/interfaces/_internal_.QueuingStrategySize.html +0 -1
- package/docs/interfaces/_internal_.ReadFileResponse.html +0 -7
- package/docs/interfaces/_internal_.ReadOnlyDict.html +0 -1
- package/docs/interfaces/_internal_.ReadWriteStream.html +0 -144
- package/docs/interfaces/_internal_.ReadableByteStreamController.html +0 -6
- package/docs/interfaces/_internal_.ReadableByteStreamControllerCallback.html +0 -1
- package/docs/interfaces/_internal_.ReadableOptions.html +0 -11
- package/docs/interfaces/_internal_.ReadableStream-1.html +0 -141
- package/docs/interfaces/_internal_.ReadableStream.html +0 -10
- package/docs/interfaces/_internal_.ReadableStreamBYOBReader.html +0 -5
- package/docs/interfaces/_internal_.ReadableStreamDefaultController.html +0 -5
- package/docs/interfaces/_internal_.ReadableStreamDefaultReadDoneResult.html +0 -3
- package/docs/interfaces/_internal_.ReadableStreamDefaultReadValueResult.html +0 -3
- package/docs/interfaces/_internal_.ReadableStreamDefaultReader.html +0 -5
- package/docs/interfaces/_internal_.ReadableStreamErrorCallback.html +0 -1
- package/docs/interfaces/_internal_.ReadableStreamGenericReader.html +0 -3
- package/docs/interfaces/_internal_.ReadableStreamReadDoneResult.html +0 -3
- package/docs/interfaces/_internal_.ReadableStreamReadValueResult.html +0 -3
- package/docs/interfaces/_internal_.ReadableWritablePair.html +0 -9
- package/docs/interfaces/_internal_.SecureContext.html +0 -2
- package/docs/interfaces/_internal_.SecureContextOptions.html +0 -121
- package/docs/interfaces/_internal_.Selection.html +0 -6
- package/docs/interfaces/_internal_.ServerOptions.html +0 -64
- package/docs/interfaces/_internal_.ServerOpts.html +0 -25
- package/docs/interfaces/_internal_.SharedArrayBuffer.html +0 -7
- package/docs/interfaces/_internal_.SharedArrayBufferConstructor.html +0 -3
- package/docs/interfaces/_internal_.SocketConstructorOpts.html +0 -6
- package/docs/interfaces/_internal_.StaticEventEmitterOptions.html +0 -15
- package/docs/interfaces/_internal_.StreamOptions.html +0 -9
- package/docs/interfaces/_internal_.StreamPipeOptions.html +0 -37
- package/docs/interfaces/_internal_.TLSSocketOptions.html +0 -160
- package/docs/interfaces/_internal_.TcpSocketConnectOpts.html +0 -19
- package/docs/interfaces/_internal_.TerminalInterruptResponse.html +0 -3
- package/docs/interfaces/_internal_.TlsOptions.html +0 -197
- package/docs/interfaces/_internal_.UnderlyingByteSource.html +0 -6
- package/docs/interfaces/_internal_.UnderlyingSink.html +0 -6
- package/docs/interfaces/_internal_.UnderlyingSinkAbortCallback.html +0 -1
- package/docs/interfaces/_internal_.UnderlyingSinkCloseCallback.html +0 -1
- package/docs/interfaces/_internal_.UnderlyingSinkStartCallback.html +0 -1
- package/docs/interfaces/_internal_.UnderlyingSinkWriteCallback.html +0 -1
- package/docs/interfaces/_internal_.UnderlyingSource.html +0 -5
- package/docs/interfaces/_internal_.UnderlyingSourceCancelCallback.html +0 -1
- package/docs/interfaces/_internal_.UnderlyingSourcePullCallback.html +0 -1
- package/docs/interfaces/_internal_.UnderlyingSourceStartCallback.html +0 -1
- package/docs/interfaces/_internal_.UpdateFileResponse.html +0 -6
- package/docs/interfaces/_internal_.UrlResponse.html +0 -3
- package/docs/interfaces/_internal_.Usage-1.html +0 -4
- package/docs/interfaces/_internal_.Usage.html +0 -4
- package/docs/interfaces/_internal_.UserMessage.html +0 -3
- package/docs/interfaces/_internal_.UserRequest.html +0 -7
- package/docs/interfaces/_internal_.WebSocket.AddressInfo.html +0 -4
- package/docs/interfaces/_internal_.WebSocket.ClientOptions.html +0 -138
- package/docs/interfaces/_internal_.WebSocket.CloseEvent.html +0 -6
- package/docs/interfaces/_internal_.WebSocket.ErrorEvent.html +0 -5
- package/docs/interfaces/_internal_.WebSocket.Event.html +0 -3
- package/docs/interfaces/_internal_.WebSocket.EventListenerOptions.html +0 -2
- package/docs/interfaces/_internal_.WebSocket.MessageEvent.html +0 -4
- package/docs/interfaces/_internal_.WebSocket.PerMessageDeflateOptions.html +0 -9
- package/docs/interfaces/_internal_.WebSocket.ServerOptions.html +0 -14
- package/docs/interfaces/_internal_.WebSocket.WebSocket.html +0 -172
- package/docs/interfaces/_internal_.WebSocket.WebSocketServer.html +0 -144
- package/docs/interfaces/_internal_.WebSocketAlias.html +0 -172
- package/docs/interfaces/_internal_.WritableOptions.html +0 -14
- package/docs/interfaces/_internal_.WritableStream-1.html +0 -8
- package/docs/interfaces/_internal_.WritableStream.html +0 -133
- package/docs/interfaces/_internal_.WritableStreamDefaultController.html +0 -6
- package/docs/interfaces/_internal_.WritableStreamDefaultWriter.html +0 -12
- package/docs/interfaces/_internal_.X509CheckOptions.html +0 -16
- package/docs/interfaces/_internal_.ZlibOptions.html +0 -21
- package/docs/interfaces/_internal_.getMatchDetail.html +0 -3
- package/docs/interfaces/_internal_.internal.FinishedOptions.html +0 -6
- package/docs/interfaces/_internal_.internal.Pipe.html +0 -5
- package/docs/interfaces/_internal_.internal.PipelineOptions.html +0 -3
- package/docs/interfaces/_internal_.internal.TransformOptions.html +0 -24
- package/docs/modules/_internal_.EventEmitter.html +0 -6
- package/docs/modules/_internal_.WebSocket.html +0 -21
- package/docs/modules/_internal_._node_stream_consumers_.html +0 -6
- package/docs/modules/_internal_._node_stream_promises_.html +0 -3
- package/docs/modules/_internal_.html +0 -228
- package/docs/modules/_internal_.internal.finished.html +0 -2
- package/docs/modules/_internal_.internal.html +0 -36
- package/docs/modules/_internal_.internal.pipeline.html +0 -2
- package/docs/modules.html +0 -3
- package/docs/types/_internal_.AnyRest.html +0 -1
- package/docs/types/_internal_.Args.html +0 -1
- package/docs/types/_internal_.ArrayBufferLike.html +0 -1
- package/docs/types/_internal_.ArrayBufferView-1.html +0 -1
- package/docs/types/_internal_.BinaryLike.html +0 -1
- package/docs/types/_internal_.BufferEncoding.html +0 -1
- package/docs/types/_internal_.BufferLike.html +0 -1
- package/docs/types/_internal_.ComposeFnParam.html +0 -1
- package/docs/types/_internal_.DefaultEventMap.html +0 -1
- package/docs/types/_internal_.EventMap.html +0 -1
- package/docs/types/_internal_.InstanceType.html +0 -2
- package/docs/types/_internal_.IteratorResult.html +0 -1
- package/docs/types/_internal_.Key.html +0 -1
- package/docs/types/_internal_.Key2.html +0 -1
- package/docs/types/_internal_.KeyFormat.html +0 -1
- package/docs/types/_internal_.KeyObjectType.html +0 -1
- package/docs/types/_internal_.KeyType-1.html +0 -1
- package/docs/types/_internal_.KeyType.html +0 -1
- package/docs/types/_internal_.KeyUsage.html +0 -1
- package/docs/types/_internal_.Listener.html +0 -1
- package/docs/types/_internal_.LookupFunction.html +0 -1
- package/docs/types/_internal_.OutgoingHttpHeader.html +0 -1
- package/docs/types/_internal_.Partial.html +0 -2
- package/docs/types/_internal_.Pick.html +0 -2
- package/docs/types/_internal_.PropertyKey.html +0 -1
- package/docs/types/_internal_.ReadableStreamController.html +0 -1
- package/docs/types/_internal_.ReadableStreamDefaultReadResult.html +0 -1
- package/docs/types/_internal_.ReadableStreamReadResult.html +0 -1
- package/docs/types/_internal_.Record.html +0 -2
- package/docs/types/_internal_.RequestListener.html +0 -1
- package/docs/types/_internal_.SecureVersion.html +0 -1
- package/docs/types/_internal_.ServerOptions-1.html +0 -1
- package/docs/types/_internal_.SocketConnectOpts.html +0 -1
- package/docs/types/_internal_.SocketReadyState.html +0 -1
- package/docs/types/_internal_.TypedArray.html +0 -1
- package/docs/types/_internal_.WebSocket.CertMeta.html +0 -2
- package/docs/types/_internal_.WebSocket.Data.html +0 -2
- package/docs/types/_internal_.WebSocket.RawData.html +0 -2
- package/docs/types/_internal_.WebSocket.VerifyClientCallbackAsync.html +0 -4
- package/docs/types/_internal_.WebSocket.VerifyClientCallbackSync.html +0 -4
- package/docs/types/_internal_.WithImplicitCoercion.html +0 -1
- package/docs/types/_internal_.internal.PipelineCallback.html +0 -1
- package/docs/types/_internal_.internal.PipelineDestination.html +0 -1
- package/docs/types/_internal_.internal.PipelineDestinationIterableFunction.html +0 -1
- package/docs/types/_internal_.internal.PipelineDestinationPromiseFunction.html +0 -1
- package/docs/types/_internal_.internal.PipelinePromise.html +0 -1
- package/docs/types/_internal_.internal.PipelineSource.html +0 -1
- package/docs/types/_internal_.internal.PipelineSourceFunction.html +0 -1
- package/docs/types/_internal_.internal.PipelineTransform.html +0 -1
- package/docs/types/_internal_.internal.PipelineTransformSource.html +0 -1
- package/docs/types/_internal_.internal.TransformCallback.html +0 -1
- package/docs/variables/_internal_.Buffer-1.html +0 -1
- package/docs/variables/_internal_.Error-1.html +0 -1
- package/docs/variables/_internal_.Object-1.html +0 -2
- package/docs/variables/_internal_.ReadableByteStreamController-1.html +0 -1
- package/docs/variables/_internal_.ReadableStream-2.html +0 -1
- package/docs/variables/_internal_.ReadableStreamBYOBReader-1.html +0 -1
- package/docs/variables/_internal_.ReadableStreamDefaultController-1.html +0 -1
- package/docs/variables/_internal_.ReadableStreamDefaultReader-1.html +0 -1
- package/docs/variables/_internal_.SharedArrayBuffer-1.html +0 -1
- package/docs/variables/_internal_.WebSocket.WebSocket-1.html +0 -1
- package/docs/variables/_internal_.WebSocket.WebSocketServer-1.html +0 -1
- package/docs/variables/_internal_.WebSocketAlias-1.html +0 -1
- package/docs/variables/_internal_.WritableStream-2.html +0 -1
- package/docs/variables/_internal_.WritableStreamDefaultController-1.html +0 -1
- package/docs/variables/_internal_.WritableStreamDefaultWriter-1.html +0 -1
- package/docs/variables/_internal_.internal.consumers.html +0 -1
- package/docs/variables/_internal_.internal.promises.html +0 -1
- package/docs/variables/default.html +0 -1
- package/script/gen.js +0 -27
- package/src/index.ts +0 -107
- package/src/modules/agent.ts +0 -120
- package/src/modules/agentlib/agent.ts +0 -406
- package/src/modules/agentlib/systemprompt.ts +0 -55
- package/src/modules/agentlib/taskInstruction.ts +0 -105
- package/src/modules/agentlib/usermessage.ts +0 -188
- package/src/modules/browser.ts +0 -352
- package/src/modules/chat.ts +0 -229
- package/src/modules/codeparsers.ts +0 -28
- package/src/modules/codeutils.ts +0 -182
- package/src/modules/crawler.ts +0 -121
- package/src/modules/dbmemory.ts +0 -52
- package/src/modules/debug.ts +0 -68
- package/src/modules/docutils.ts +0 -18
- package/src/modules/fs.ts +0 -263
- package/src/modules/git.ts +0 -237
- package/src/modules/history.ts +0 -82
- package/src/modules/knowledge.ts +0 -5
- package/src/modules/llm.ts +0 -36
- package/src/modules/outputparsers.ts +0 -30
- package/src/modules/project.ts +0 -68
- package/src/modules/rag.ts +0 -27
- package/src/modules/search.ts +0 -32
- package/src/modules/state.ts +0 -115
- package/src/modules/task.ts +0 -73
- package/src/modules/terminal.ts +0 -113
- package/src/modules/tokenizer.ts +0 -56
- package/src/modules/toolBox.ts +0 -1384
- package/src/modules/tools.ts +0 -254
- package/src/modules/vectordb.ts +0 -102
- package/src/modules/websocket.ts +0 -97
- package/src/utils.ts +0 -5
- package/tests/chat.test.js +0 -0
- package/tests/fs.test.js +0 -89
- package/tests/index.test.js +0 -4
- package/tsconfig.json +0 -122
- package/utils.d.ts +0 -5
- package/webpack.config.js +0 -25
- /package/{modules → dist}/agentlib/agent.d.ts +0 -0
- /package/{modules → dist}/agentlib/systemprompt.d.ts +0 -0
- /package/{modules → dist}/agentlib/systemprompt.js +0 -0
- /package/{modules → dist}/agentlib/taskInstruction.d.ts +0 -0
- /package/{modules → dist}/agentlib/taskInstruction.js +0 -0
- /package/{modules → dist}/agentlib/usermessage.d.ts +0 -0
- /package/{modules → dist/modules}/agent.d.ts +0 -0
- /package/{modules → dist/modules}/agent.js +0 -0
- /package/{modules → dist/modules}/browser.d.ts +0 -0
- /package/{modules → dist/modules}/browser.js +0 -0
- /package/{modules → dist/modules}/chat.d.ts +0 -0
- /package/{modules → dist/modules}/chat.js +0 -0
- /package/{modules → dist/modules}/codeparsers.d.ts +0 -0
- /package/{modules → dist/modules}/codeparsers.js +0 -0
- /package/{modules → dist/modules}/codeutils.d.ts +0 -0
- /package/{modules → dist/modules}/codeutils.js +0 -0
- /package/{modules → dist/modules}/crawler.d.ts +0 -0
- /package/{modules → dist/modules}/crawler.js +0 -0
- /package/{modules → dist/modules}/dbmemory.d.ts +0 -0
- /package/{modules → dist/modules}/dbmemory.js +0 -0
- /package/{modules → dist/modules}/debug.d.ts +0 -0
- /package/{modules → dist/modules}/debug.js +0 -0
- /package/{modules → dist/modules}/docutils.d.ts +0 -0
- /package/{modules → dist/modules}/docutils.js +0 -0
- /package/{modules → dist/modules}/fs.d.ts +0 -0
- /package/{modules → dist/modules}/fs.js +0 -0
- /package/{modules → dist/modules}/git.d.ts +0 -0
- /package/{modules → dist/modules}/git.js +0 -0
- /package/{modules → dist/modules}/history.d.ts +0 -0
- /package/{modules → dist/modules}/history.js +0 -0
- /package/{modules → dist/modules}/knowledge.d.ts +0 -0
- /package/{modules → dist/modules}/knowledge.js +0 -0
- /package/{modules → dist/modules}/llm.d.ts +0 -0
- /package/{modules → dist/modules}/llm.js +0 -0
- /package/{modules → dist/modules}/outputparsers.d.ts +0 -0
- /package/{modules → dist/modules}/outputparsers.js +0 -0
- /package/{modules → dist/modules}/project.d.ts +0 -0
- /package/{modules → dist/modules}/project.js +0 -0
- /package/{modules → dist/modules}/rag.d.ts +0 -0
- /package/{modules → dist/modules}/rag.js +0 -0
- /package/{modules → dist/modules}/search.d.ts +0 -0
- /package/{modules → dist/modules}/search.js +0 -0
- /package/{modules → dist/modules}/state.d.ts +0 -0
- /package/{modules → dist/modules}/state.js +0 -0
- /package/{modules → dist/modules}/task.d.ts +0 -0
- /package/{modules → dist/modules}/task.js +0 -0
- /package/{modules → dist/modules}/terminal.d.ts +0 -0
- /package/{modules → dist/modules}/terminal.js +0 -0
- /package/{modules → dist/modules}/tokenizer.d.ts +0 -0
- /package/{modules → dist/modules}/tokenizer.js +0 -0
- /package/{modules → dist/modules}/toolBox.d.ts +0 -0
- /package/{modules → dist/modules}/toolBox.js +0 -0
- /package/{modules → dist/modules}/tools.d.ts +0 -0
- /package/{modules → dist/modules}/tools.js +0 -0
- /package/{modules → dist/modules}/vectordb.d.ts +0 -0
- /package/{modules → dist/modules}/vectordb.js +0 -0
- /package/{modules → dist/modules}/websocket.d.ts +0 -0
- /package/{modules → dist/modules}/websocket.js +0 -0
package/src/modules/toolBox.ts
DELETED
|
@@ -1,1384 +0,0 @@
|
|
|
1
|
-
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
2
|
-
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
|
-
import {
|
|
4
|
-
CallToolRequestSchema,
|
|
5
|
-
ClientCapabilities,
|
|
6
|
-
CompleteRequestSchema,
|
|
7
|
-
CreateMessageRequestSchema,
|
|
8
|
-
ErrorCode,
|
|
9
|
-
GetPromptRequestSchema,
|
|
10
|
-
ListPromptsRequestSchema,
|
|
11
|
-
ListResourcesRequestSchema,
|
|
12
|
-
ListResourceTemplatesRequestSchema,
|
|
13
|
-
ListToolsRequestSchema,
|
|
14
|
-
McpError,
|
|
15
|
-
ReadResourceRequestSchema,
|
|
16
|
-
Root,
|
|
17
|
-
RootsListChangedNotificationSchema,
|
|
18
|
-
ServerCapabilities,
|
|
19
|
-
SetLevelRequestSchema,
|
|
20
|
-
} from "@modelcontextprotocol/sdk/types.js";
|
|
21
|
-
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
22
|
-
import { z } from "zod";
|
|
23
|
-
import { setTimeout as delay } from "timers/promises";
|
|
24
|
-
import { readFile } from "fs/promises";
|
|
25
|
-
import { fileTypeFromBuffer } from "file-type";
|
|
26
|
-
import { StrictEventEmitter } from "strict-event-emitter-types";
|
|
27
|
-
import { EventEmitter } from "events";
|
|
28
|
-
import Fuse from "fuse.js";
|
|
29
|
-
import { startSSEServer } from "mcp-proxy";
|
|
30
|
-
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
31
|
-
import parseURITemplate from "uri-templates";
|
|
32
|
-
import http from "http";
|
|
33
|
-
import { fetch } from "undici";
|
|
34
|
-
import { loadEsm } from "load-esm";
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Type definition for SSE Server that can be closed.
|
|
38
|
-
*/
|
|
39
|
-
export type SSEServer = {
|
|
40
|
-
close: () => Promise<void>;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Event types for the FastMCP event emitter.
|
|
45
|
-
*/
|
|
46
|
-
type FastMCPEvents<T extends FastMCPSessionAuth> = {
|
|
47
|
-
connect: (event: { session: FastMCPSession<T> }) => void;
|
|
48
|
-
disconnect: (event: { session: FastMCPSession<T> }) => void;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Event types for FastMCPSession event emitter.
|
|
53
|
-
*/
|
|
54
|
-
type FastMCPSessionEvents = {
|
|
55
|
-
rootsChanged: (event: { roots: Root[] }) => void;
|
|
56
|
-
error: (event: { error: Error }) => void;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Generates an image content object from a URL, file path, or buffer.
|
|
61
|
-
*
|
|
62
|
-
* @param input - The input source for the image (URL, file path, or buffer)
|
|
63
|
-
* @returns Promise with the image content object
|
|
64
|
-
*/
|
|
65
|
-
export const imageContent = async (
|
|
66
|
-
input: { url: string } | { path: string } | { buffer: Buffer },
|
|
67
|
-
): Promise<ImageContent> => {
|
|
68
|
-
let rawData: Buffer;
|
|
69
|
-
|
|
70
|
-
if ("url" in input) {
|
|
71
|
-
const response = await fetch(input.url);
|
|
72
|
-
|
|
73
|
-
if (!response.ok) {
|
|
74
|
-
throw new Error(`Failed to fetch image from URL: ${response.statusText}`);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
rawData = Buffer.from(await response.arrayBuffer());
|
|
78
|
-
} else if ("path" in input) {
|
|
79
|
-
rawData = await readFile(input.path);
|
|
80
|
-
} else if ("buffer" in input) {
|
|
81
|
-
rawData = input.buffer;
|
|
82
|
-
} else {
|
|
83
|
-
throw new Error(
|
|
84
|
-
"Invalid input: Provide a valid 'url', 'path', or 'buffer'",
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const { fileTypeFromBuffer } = await loadEsm('file-type');
|
|
89
|
-
const mimeType = await fileTypeFromBuffer(rawData);
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const base64Data = rawData.toString("base64");
|
|
93
|
-
|
|
94
|
-
return {
|
|
95
|
-
type: "image",
|
|
96
|
-
data: base64Data,
|
|
97
|
-
mimeType: mimeType?.mime ?? "image/png",
|
|
98
|
-
} as const;
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Base class for FastMCP errors.
|
|
103
|
-
*/
|
|
104
|
-
abstract class FastMCPError extends Error {
|
|
105
|
-
public constructor(message?: string) {
|
|
106
|
-
super(message);
|
|
107
|
-
this.name = new.target.name;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Type for extra data in errors.
|
|
113
|
-
*/
|
|
114
|
-
type Extra = unknown;
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Type for a record of extra data.
|
|
118
|
-
*/
|
|
119
|
-
type Extras = Record<string, Extra>;
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Error class for unexpected state conditions.
|
|
123
|
-
*/
|
|
124
|
-
export class UnexpectedStateError extends FastMCPError {
|
|
125
|
-
/** Additional context for the error */
|
|
126
|
-
public extras?: Extras;
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Creates a new UnexpectedStateError.
|
|
130
|
-
*
|
|
131
|
-
* @param message - Error message
|
|
132
|
-
* @param extras - Additional context for the error
|
|
133
|
-
*/
|
|
134
|
-
public constructor(message: string, extras?: Extras) {
|
|
135
|
-
super(message);
|
|
136
|
-
this.name = new.target.name;
|
|
137
|
-
this.extras = extras;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Error that is meant to be surfaced to the user.
|
|
143
|
-
*/
|
|
144
|
-
export class UserError extends UnexpectedStateError {}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Type for tool parameters schema.
|
|
148
|
-
*/
|
|
149
|
-
type ToolParameters = z.ZodTypeAny;
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Type for literal values.
|
|
153
|
-
*/
|
|
154
|
-
type Literal = boolean | null | number | string | undefined;
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Type for serializable values that can be passed around.
|
|
158
|
-
*/
|
|
159
|
-
type SerializableValue =
|
|
160
|
-
| Literal
|
|
161
|
-
| SerializableValue[]
|
|
162
|
-
| { [key: string]: SerializableValue };
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Type for reporting progress of operations.
|
|
166
|
-
*/
|
|
167
|
-
type Progress = {
|
|
168
|
-
/**
|
|
169
|
-
* The progress thus far. This should increase every time progress is made, even if the total is unknown.
|
|
170
|
-
*/
|
|
171
|
-
progress: number;
|
|
172
|
-
/**
|
|
173
|
-
* Total number of items to process (or total progress required), if known.
|
|
174
|
-
*/
|
|
175
|
-
total?: number;
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Context object passed to tool execution functions.
|
|
180
|
-
*/
|
|
181
|
-
type Context<T extends FastMCPSessionAuth> = {
|
|
182
|
-
/** The session authentication context */
|
|
183
|
-
session: T | undefined;
|
|
184
|
-
/** Function to report progress of operations */
|
|
185
|
-
reportProgress: (progress: Progress) => Promise<void>;
|
|
186
|
-
/** Logging functions */
|
|
187
|
-
log: {
|
|
188
|
-
debug: (message: string, data?: SerializableValue) => void;
|
|
189
|
-
error: (message: string, data?: SerializableValue) => void;
|
|
190
|
-
info: (message: string, data?: SerializableValue) => void;
|
|
191
|
-
warn: (message: string, data?: SerializableValue) => void;
|
|
192
|
-
};
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Type for text content in messages.
|
|
197
|
-
*/
|
|
198
|
-
type TextContent = {
|
|
199
|
-
/** Always "text" for text content */
|
|
200
|
-
type: "text";
|
|
201
|
-
/** The text content */
|
|
202
|
-
text: string;
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
const TextContentZodSchema = z
|
|
206
|
-
.object({
|
|
207
|
-
type: z.literal("text"),
|
|
208
|
-
/**
|
|
209
|
-
* The text content of the message.
|
|
210
|
-
*/
|
|
211
|
-
text: z.string(),
|
|
212
|
-
})
|
|
213
|
-
.strict() satisfies z.ZodType<TextContent>;
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Type for image content in messages.
|
|
217
|
-
*/
|
|
218
|
-
type ImageContent = {
|
|
219
|
-
/** Always "image" for image content */
|
|
220
|
-
type: "image";
|
|
221
|
-
/** Base64-encoded image data */
|
|
222
|
-
data: string;
|
|
223
|
-
/** The MIME type of the image */
|
|
224
|
-
mimeType: string;
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
const ImageContentZodSchema = z
|
|
228
|
-
.object({
|
|
229
|
-
type: z.literal("image"),
|
|
230
|
-
/**
|
|
231
|
-
* The base64-encoded image data.
|
|
232
|
-
*/
|
|
233
|
-
data: z.string().base64(),
|
|
234
|
-
/**
|
|
235
|
-
* The MIME type of the image. Different providers may support different image types.
|
|
236
|
-
*/
|
|
237
|
-
mimeType: z.string(),
|
|
238
|
-
})
|
|
239
|
-
.strict() satisfies z.ZodType<ImageContent>;
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Union type for content in messages.
|
|
243
|
-
*/
|
|
244
|
-
type Content = TextContent | ImageContent;
|
|
245
|
-
|
|
246
|
-
const ContentZodSchema = z.discriminatedUnion("type", [
|
|
247
|
-
TextContentZodSchema,
|
|
248
|
-
ImageContentZodSchema,
|
|
249
|
-
]) satisfies z.ZodType<Content>;
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Type for content results from tool executions.
|
|
253
|
-
*/
|
|
254
|
-
type ContentResult = {
|
|
255
|
-
/** Array of content blocks */
|
|
256
|
-
content: Content[];
|
|
257
|
-
/** Whether this result represents an error */
|
|
258
|
-
isError?: boolean;
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
const ContentResultZodSchema = z
|
|
262
|
-
.object({
|
|
263
|
-
content: ContentZodSchema.array(),
|
|
264
|
-
isError: z.boolean().optional(),
|
|
265
|
-
})
|
|
266
|
-
.strict() satisfies z.ZodType<ContentResult>;
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Type for completion results.
|
|
270
|
-
*/
|
|
271
|
-
type Completion = {
|
|
272
|
-
/** Array of completion values */
|
|
273
|
-
values: string[];
|
|
274
|
-
/** The total number of completions available */
|
|
275
|
-
total?: number;
|
|
276
|
-
/** Whether there are more completions available */
|
|
277
|
-
hasMore?: boolean;
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* Schema for completion results.
|
|
282
|
-
* https://github.com/modelcontextprotocol/typescript-sdk/blob/3164da64d085ec4e022ae881329eee7b72f208d4/src/types.ts#L983-L1003
|
|
283
|
-
*/
|
|
284
|
-
const CompletionZodSchema = z.object({
|
|
285
|
-
/**
|
|
286
|
-
* An array of completion values. Must not exceed 100 items.
|
|
287
|
-
*/
|
|
288
|
-
values: z.array(z.string()).max(100),
|
|
289
|
-
/**
|
|
290
|
-
* The total number of completion options available. This can exceed the number of values actually sent in the response.
|
|
291
|
-
*/
|
|
292
|
-
total: z.optional(z.number().int()),
|
|
293
|
-
/**
|
|
294
|
-
* Indicates whether there are additional completion options beyond those provided in the current response, even if the exact total is unknown.
|
|
295
|
-
*/
|
|
296
|
-
hasMore: z.optional(z.boolean()),
|
|
297
|
-
}) satisfies z.ZodType<Completion>;
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* Type for a tool that can be executed.
|
|
301
|
-
*/
|
|
302
|
-
type Tool<T extends FastMCPSessionAuth, Params extends ToolParameters = ToolParameters> = {
|
|
303
|
-
/** Name of the tool */
|
|
304
|
-
name: string;
|
|
305
|
-
/** Optional description of the tool's functionality */
|
|
306
|
-
description?: string;
|
|
307
|
-
/** Optional parameters schema for the tool */
|
|
308
|
-
parameters?: Params;
|
|
309
|
-
/** Function to execute the tool */
|
|
310
|
-
execute: (
|
|
311
|
-
args: z.infer<Params>,
|
|
312
|
-
context: Context<T>,
|
|
313
|
-
) => Promise<string | ContentResult | TextContent | ImageContent>;
|
|
314
|
-
};
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* Type for resource results.
|
|
318
|
-
*/
|
|
319
|
-
type ResourceResult =
|
|
320
|
-
| {
|
|
321
|
-
text: string;
|
|
322
|
-
}
|
|
323
|
-
| {
|
|
324
|
-
blob: string;
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* Type for input resource template arguments.
|
|
329
|
-
*/
|
|
330
|
-
type InputResourceTemplateArgument = Readonly<{
|
|
331
|
-
name: string;
|
|
332
|
-
description?: string;
|
|
333
|
-
complete?: ArgumentValueCompleter;
|
|
334
|
-
}>;
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* Type for resource template arguments.
|
|
338
|
-
*/
|
|
339
|
-
type ResourceTemplateArgument = Readonly<{
|
|
340
|
-
name: string;
|
|
341
|
-
description?: string;
|
|
342
|
-
complete?: ArgumentValueCompleter;
|
|
343
|
-
}>;
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Type for resource templates.
|
|
347
|
-
*/
|
|
348
|
-
type ResourceTemplate<
|
|
349
|
-
Arguments extends ResourceTemplateArgument[] = ResourceTemplateArgument[],
|
|
350
|
-
> = {
|
|
351
|
-
/** URI template for the resource */
|
|
352
|
-
uriTemplate: string;
|
|
353
|
-
/** Name of the resource template */
|
|
354
|
-
name: string;
|
|
355
|
-
/** Optional description */
|
|
356
|
-
description?: string;
|
|
357
|
-
/** Optional MIME type */
|
|
358
|
-
mimeType?: string;
|
|
359
|
-
/** Arguments for the template */
|
|
360
|
-
arguments: Arguments;
|
|
361
|
-
/** Optional completion function */
|
|
362
|
-
complete?: (name: string, value: string) => Promise<Completion>;
|
|
363
|
-
/** Function to load the resource */
|
|
364
|
-
load: (
|
|
365
|
-
args: ResourceTemplateArgumentsToObject<Arguments>,
|
|
366
|
-
) => Promise<ResourceResult>;
|
|
367
|
-
};
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* Type transformation from argument array to object.
|
|
371
|
-
*/
|
|
372
|
-
type ResourceTemplateArgumentsToObject<T extends { name: string }[]> = {
|
|
373
|
-
[K in T[number]["name"]]: string;
|
|
374
|
-
};
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* Type for input resource templates.
|
|
378
|
-
*/
|
|
379
|
-
type InputResourceTemplate<
|
|
380
|
-
Arguments extends ResourceTemplateArgument[] = ResourceTemplateArgument[],
|
|
381
|
-
> = {
|
|
382
|
-
/** URI template for the resource */
|
|
383
|
-
uriTemplate: string;
|
|
384
|
-
/** Name of the template */
|
|
385
|
-
name: string;
|
|
386
|
-
/** Optional description */
|
|
387
|
-
description?: string;
|
|
388
|
-
/** Optional MIME type */
|
|
389
|
-
mimeType?: string;
|
|
390
|
-
/** Arguments for the template */
|
|
391
|
-
arguments: Arguments;
|
|
392
|
-
/** Function to load the resource */
|
|
393
|
-
load: (
|
|
394
|
-
args: ResourceTemplateArgumentsToObject<Arguments>,
|
|
395
|
-
) => Promise<ResourceResult>;
|
|
396
|
-
};
|
|
397
|
-
|
|
398
|
-
/**
|
|
399
|
-
* Type for a resource.
|
|
400
|
-
*/
|
|
401
|
-
type Resource = {
|
|
402
|
-
/** URI of the resource */
|
|
403
|
-
uri: string;
|
|
404
|
-
/** Name of the resource */
|
|
405
|
-
name: string;
|
|
406
|
-
/** Optional description */
|
|
407
|
-
description?: string;
|
|
408
|
-
/** Optional MIME type */
|
|
409
|
-
mimeType?: string;
|
|
410
|
-
/** Function to load the resource */
|
|
411
|
-
load: () => Promise<ResourceResult | ResourceResult[]>;
|
|
412
|
-
/** Optional completion function */
|
|
413
|
-
complete?: (name: string, value: string) => Promise<Completion>;
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
/**
|
|
417
|
-
* Type for argument value completion.
|
|
418
|
-
*/
|
|
419
|
-
type ArgumentValueCompleter = (value: string) => Promise<Completion>;
|
|
420
|
-
|
|
421
|
-
/**
|
|
422
|
-
* Type for input prompt arguments.
|
|
423
|
-
*/
|
|
424
|
-
type InputPromptArgument = Readonly<{
|
|
425
|
-
/** Name of the argument */
|
|
426
|
-
name: string;
|
|
427
|
-
/** Optional description */
|
|
428
|
-
description?: string;
|
|
429
|
-
/** Whether the argument is required */
|
|
430
|
-
required?: boolean;
|
|
431
|
-
/** Optional completion function */
|
|
432
|
-
complete?: ArgumentValueCompleter;
|
|
433
|
-
/** Optional enum of possible values */
|
|
434
|
-
enum?: string[];
|
|
435
|
-
}>;
|
|
436
|
-
|
|
437
|
-
/**
|
|
438
|
-
* Type transformation from prompt arguments to object.
|
|
439
|
-
*/
|
|
440
|
-
type PromptArgumentsToObject<T extends { name: string; required?: boolean }[]> =
|
|
441
|
-
{
|
|
442
|
-
[K in T[number]["name"]]: Extract<
|
|
443
|
-
T[number],
|
|
444
|
-
{ name: K }
|
|
445
|
-
>["required"] extends true
|
|
446
|
-
? string
|
|
447
|
-
: string | undefined;
|
|
448
|
-
};
|
|
449
|
-
|
|
450
|
-
/**
|
|
451
|
-
* Type for input prompts.
|
|
452
|
-
*/
|
|
453
|
-
type InputPrompt<
|
|
454
|
-
Arguments extends InputPromptArgument[] = InputPromptArgument[],
|
|
455
|
-
Args = PromptArgumentsToObject<Arguments>,
|
|
456
|
-
> = {
|
|
457
|
-
/** Name of the prompt */
|
|
458
|
-
name: string;
|
|
459
|
-
/** Optional description */
|
|
460
|
-
description?: string;
|
|
461
|
-
/** Optional arguments */
|
|
462
|
-
arguments?: InputPromptArgument[];
|
|
463
|
-
/** Function to load the prompt */
|
|
464
|
-
load: (args: Args) => Promise<string>;
|
|
465
|
-
};
|
|
466
|
-
|
|
467
|
-
/**
|
|
468
|
-
* Type for prompt arguments.
|
|
469
|
-
*/
|
|
470
|
-
type PromptArgument = Readonly<{
|
|
471
|
-
/** Name of the argument */
|
|
472
|
-
name: string;
|
|
473
|
-
/** Optional description */
|
|
474
|
-
description?: string;
|
|
475
|
-
/** Whether the argument is required */
|
|
476
|
-
required?: boolean;
|
|
477
|
-
/** Optional completion function */
|
|
478
|
-
complete?: ArgumentValueCompleter;
|
|
479
|
-
/** Optional enum of possible values */
|
|
480
|
-
enum?: string[];
|
|
481
|
-
}>;
|
|
482
|
-
|
|
483
|
-
/**
|
|
484
|
-
* Type for prompts.
|
|
485
|
-
*/
|
|
486
|
-
type Prompt<
|
|
487
|
-
Arguments extends PromptArgument[] = PromptArgument[],
|
|
488
|
-
Args = PromptArgumentsToObject<Arguments>,
|
|
489
|
-
> = {
|
|
490
|
-
/** Optional arguments */
|
|
491
|
-
arguments?: PromptArgument[];
|
|
492
|
-
/** Optional completion function */
|
|
493
|
-
complete?: (name: string, value: string) => Promise<Completion>;
|
|
494
|
-
/** Optional description */
|
|
495
|
-
description?: string;
|
|
496
|
-
/** Function to load the prompt */
|
|
497
|
-
load: (args: Args) => Promise<string>;
|
|
498
|
-
/** Name of the prompt */
|
|
499
|
-
name: string;
|
|
500
|
-
};
|
|
501
|
-
|
|
502
|
-
/**
|
|
503
|
-
* Type for server options.
|
|
504
|
-
*/
|
|
505
|
-
type ServerOptions<T extends FastMCPSessionAuth> = {
|
|
506
|
-
/** Name of the server */
|
|
507
|
-
name: string;
|
|
508
|
-
/** Version of the server */
|
|
509
|
-
version: `${number}.${number}.${number}`;
|
|
510
|
-
/** Optional authentication function */
|
|
511
|
-
authenticate?: Authenticate<T>;
|
|
512
|
-
};
|
|
513
|
-
|
|
514
|
-
/**
|
|
515
|
-
* Type for logging levels.
|
|
516
|
-
*/
|
|
517
|
-
type LoggingLevel =
|
|
518
|
-
| "debug"
|
|
519
|
-
| "info"
|
|
520
|
-
| "notice"
|
|
521
|
-
| "warning"
|
|
522
|
-
| "error"
|
|
523
|
-
| "critical"
|
|
524
|
-
| "alert"
|
|
525
|
-
| "emergency";
|
|
526
|
-
|
|
527
|
-
const FastMCPSessionEventEmitterBase: {
|
|
528
|
-
new (): StrictEventEmitter<EventEmitter, FastMCPSessionEvents>;
|
|
529
|
-
} = EventEmitter;
|
|
530
|
-
|
|
531
|
-
class FastMCPSessionEventEmitter extends FastMCPSessionEventEmitterBase {}
|
|
532
|
-
|
|
533
|
-
/**
|
|
534
|
-
* Type for sampling responses.
|
|
535
|
-
*/
|
|
536
|
-
type SamplingResponse = {
|
|
537
|
-
/** The model used */
|
|
538
|
-
model: string;
|
|
539
|
-
/** Optional reason for stopping */
|
|
540
|
-
stopReason?: "endTurn" | "stopSequence" | "maxTokens" | string;
|
|
541
|
-
/** Role of the message */
|
|
542
|
-
role: "user" | "assistant";
|
|
543
|
-
/** Content of the message */
|
|
544
|
-
content: TextContent | ImageContent;
|
|
545
|
-
};
|
|
546
|
-
|
|
547
|
-
/**
|
|
548
|
-
* Type for session authentication.
|
|
549
|
-
*/
|
|
550
|
-
type FastMCPSessionAuth = Record<string, unknown> | undefined;
|
|
551
|
-
|
|
552
|
-
/**
|
|
553
|
-
* Class representing a FastMCP session.
|
|
554
|
-
* Manages communication between the client and server.
|
|
555
|
-
*/
|
|
556
|
-
export class FastMCPSession<T extends FastMCPSessionAuth = FastMCPSessionAuth> extends FastMCPSessionEventEmitter {
|
|
557
|
-
#capabilities: ServerCapabilities = {};
|
|
558
|
-
#clientCapabilities?: ClientCapabilities;
|
|
559
|
-
#loggingLevel: LoggingLevel = "info";
|
|
560
|
-
#prompts: Prompt[] = [];
|
|
561
|
-
#resources: Resource[] = [];
|
|
562
|
-
#resourceTemplates: ResourceTemplate[] = [];
|
|
563
|
-
#roots: Root[] = [];
|
|
564
|
-
#server: Server;
|
|
565
|
-
#auth: T | undefined;
|
|
566
|
-
|
|
567
|
-
/**
|
|
568
|
-
* Creates a new FastMCPSession.
|
|
569
|
-
*
|
|
570
|
-
* @param options - Configuration options for the session
|
|
571
|
-
*/
|
|
572
|
-
constructor({
|
|
573
|
-
auth,
|
|
574
|
-
name,
|
|
575
|
-
version,
|
|
576
|
-
tools,
|
|
577
|
-
resources,
|
|
578
|
-
resourcesTemplates,
|
|
579
|
-
prompts,
|
|
580
|
-
}: {
|
|
581
|
-
auth?: T;
|
|
582
|
-
name: string;
|
|
583
|
-
version: string;
|
|
584
|
-
tools: Tool<T>[];
|
|
585
|
-
resources: Resource[];
|
|
586
|
-
resourcesTemplates: InputResourceTemplate[];
|
|
587
|
-
prompts: Prompt[];
|
|
588
|
-
}) {
|
|
589
|
-
super();
|
|
590
|
-
|
|
591
|
-
this.#auth = auth;
|
|
592
|
-
|
|
593
|
-
if (tools.length) {
|
|
594
|
-
this.#capabilities.tools = {};
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
if (resources.length || resourcesTemplates.length) {
|
|
598
|
-
this.#capabilities.resources = {};
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
if (prompts.length) {
|
|
602
|
-
for (const prompt of prompts) {
|
|
603
|
-
this.addPrompt(prompt);
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
this.#capabilities.prompts = {};
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
this.#capabilities.logging = {};
|
|
610
|
-
|
|
611
|
-
this.#server = new Server(
|
|
612
|
-
{ name: name, version: version },
|
|
613
|
-
{ capabilities: this.#capabilities },
|
|
614
|
-
);
|
|
615
|
-
|
|
616
|
-
this.setupErrorHandling();
|
|
617
|
-
this.setupLoggingHandlers();
|
|
618
|
-
this.setupRootsHandlers();
|
|
619
|
-
this.setupCompleteHandlers();
|
|
620
|
-
|
|
621
|
-
if (tools.length) {
|
|
622
|
-
this.setupToolHandlers(tools);
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
if (resources.length || resourcesTemplates.length) {
|
|
626
|
-
for (const resource of resources) {
|
|
627
|
-
this.addResource(resource);
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
this.setupResourceHandlers(resources);
|
|
631
|
-
|
|
632
|
-
if (resourcesTemplates.length) {
|
|
633
|
-
for (const resourceTemplate of resourcesTemplates) {
|
|
634
|
-
this.addResourceTemplate(resourceTemplate);
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
this.setupResourceTemplateHandlers(resourcesTemplates);
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
if (prompts.length) {
|
|
642
|
-
this.setupPromptHandlers(prompts);
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
private addResource(inputResource: Resource) {
|
|
647
|
-
this.#resources.push(inputResource);
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
private addResourceTemplate(inputResourceTemplate: InputResourceTemplate) {
|
|
651
|
-
const completers: Record<string, ArgumentValueCompleter> = {};
|
|
652
|
-
|
|
653
|
-
for (const argument of inputResourceTemplate.arguments ?? []) {
|
|
654
|
-
if (argument.complete) {
|
|
655
|
-
completers[argument.name] = argument.complete;
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
const resourceTemplate = {
|
|
660
|
-
...inputResourceTemplate,
|
|
661
|
-
complete: async (name: string, value: string) => {
|
|
662
|
-
if (completers[name]) {
|
|
663
|
-
return await completers[name](value);
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
return {
|
|
667
|
-
values: [],
|
|
668
|
-
};
|
|
669
|
-
},
|
|
670
|
-
};
|
|
671
|
-
|
|
672
|
-
this.#resourceTemplates.push(resourceTemplate);
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
private addPrompt(inputPrompt: InputPrompt) {
|
|
676
|
-
const completers: Record<string, ArgumentValueCompleter> = {};
|
|
677
|
-
const enums: Record<string, string[]> = {};
|
|
678
|
-
|
|
679
|
-
for (const argument of inputPrompt.arguments ?? []) {
|
|
680
|
-
if (argument.complete) {
|
|
681
|
-
completers[argument.name] = argument.complete;
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
if (argument.enum) {
|
|
685
|
-
enums[argument.name] = argument.enum;
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
const prompt = {
|
|
690
|
-
...inputPrompt,
|
|
691
|
-
complete: async (name: string, value: string) => {
|
|
692
|
-
if (completers[name]) {
|
|
693
|
-
return await completers[name](value);
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
if (enums[name]) {
|
|
697
|
-
const fuse = new Fuse(enums[name], {
|
|
698
|
-
keys: ["value"],
|
|
699
|
-
});
|
|
700
|
-
|
|
701
|
-
const result = fuse.search(value);
|
|
702
|
-
|
|
703
|
-
return {
|
|
704
|
-
values: result.map((item) => item.item),
|
|
705
|
-
total: result.length,
|
|
706
|
-
};
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
return {
|
|
710
|
-
values: [],
|
|
711
|
-
};
|
|
712
|
-
},
|
|
713
|
-
};
|
|
714
|
-
|
|
715
|
-
this.#prompts.push(prompt);
|
|
716
|
-
}
|
|
717
|
-
|
|
718
|
-
public get clientCapabilities(): ClientCapabilities | null {
|
|
719
|
-
return this.#clientCapabilities ?? null;
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
public get server(): Server {
|
|
723
|
-
return this.#server;
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
#pingInterval: ReturnType<typeof setInterval> | null = null;
|
|
727
|
-
|
|
728
|
-
public async requestSampling(
|
|
729
|
-
message: z.infer<typeof CreateMessageRequestSchema>["params"],
|
|
730
|
-
): Promise<SamplingResponse> {
|
|
731
|
-
return this.#server.createMessage(message) as unknown as SamplingResponse;
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
public async connect(transport: Transport) {
|
|
735
|
-
if (this.#server.transport) {
|
|
736
|
-
throw new UnexpectedStateError("Server is already connected");
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
await this.#server.connect(transport);
|
|
740
|
-
|
|
741
|
-
let attempt = 0;
|
|
742
|
-
|
|
743
|
-
while (attempt++ < 10) {
|
|
744
|
-
const capabilities = await this.#server.getClientCapabilities();
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
if (capabilities) {
|
|
748
|
-
this.#clientCapabilities = capabilities;
|
|
749
|
-
|
|
750
|
-
break;
|
|
751
|
-
}
|
|
752
|
-
|
|
753
|
-
await delay(100);
|
|
754
|
-
}
|
|
755
|
-
|
|
756
|
-
if (!this.#clientCapabilities) {
|
|
757
|
-
// console.warn('[warning] toolBox could not infer client capabilities')
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
if (this.#clientCapabilities?.roots) {
|
|
761
|
-
const roots = await this.#server.listRoots();
|
|
762
|
-
|
|
763
|
-
this.#roots = roots.roots;
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
this.#pingInterval = setInterval(async () => {
|
|
767
|
-
try {
|
|
768
|
-
await this.#server.ping();
|
|
769
|
-
} catch (error) {
|
|
770
|
-
this.emit("error", {
|
|
771
|
-
error: error as Error,
|
|
772
|
-
});
|
|
773
|
-
}
|
|
774
|
-
}, 1000);
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
public get roots(): Root[] {
|
|
778
|
-
return this.#roots;
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
public async close() {
|
|
782
|
-
if (this.#pingInterval) {
|
|
783
|
-
clearInterval(this.#pingInterval);
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
try {
|
|
787
|
-
await this.#server.close();
|
|
788
|
-
} catch (error) {
|
|
789
|
-
console.error("[MCP Error]", "could not close server", error);
|
|
790
|
-
}
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
private setupErrorHandling() {
|
|
794
|
-
this.#server.onerror = (error) => {
|
|
795
|
-
console.error("[MCP Error]", error);
|
|
796
|
-
};
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
public get loggingLevel(): LoggingLevel {
|
|
800
|
-
return this.#loggingLevel;
|
|
801
|
-
}
|
|
802
|
-
|
|
803
|
-
private setupCompleteHandlers() {
|
|
804
|
-
this.#server.setRequestHandler(CompleteRequestSchema, async (request) => {
|
|
805
|
-
if (request.params.ref.type === "ref/prompt") {
|
|
806
|
-
const prompt = this.#prompts.find(
|
|
807
|
-
(prompt) => prompt.name === request.params.ref.name,
|
|
808
|
-
);
|
|
809
|
-
|
|
810
|
-
if (!prompt) {
|
|
811
|
-
throw new UnexpectedStateError("Unknown prompt", {
|
|
812
|
-
request,
|
|
813
|
-
});
|
|
814
|
-
}
|
|
815
|
-
|
|
816
|
-
if (!prompt.complete) {
|
|
817
|
-
throw new UnexpectedStateError("Prompt does not support completion", {
|
|
818
|
-
request,
|
|
819
|
-
});
|
|
820
|
-
}
|
|
821
|
-
|
|
822
|
-
const completion = CompletionZodSchema.parse(
|
|
823
|
-
await prompt.complete(
|
|
824
|
-
request.params.argument.name,
|
|
825
|
-
request.params.argument.value,
|
|
826
|
-
),
|
|
827
|
-
);
|
|
828
|
-
|
|
829
|
-
return {
|
|
830
|
-
completion,
|
|
831
|
-
};
|
|
832
|
-
}
|
|
833
|
-
|
|
834
|
-
if (request.params.ref.type === "ref/resource") {
|
|
835
|
-
const resource = this.#resourceTemplates.find(
|
|
836
|
-
(resource) => resource.uriTemplate === request.params.ref.uri,
|
|
837
|
-
);
|
|
838
|
-
|
|
839
|
-
if (!resource) {
|
|
840
|
-
throw new UnexpectedStateError("Unknown resource", {
|
|
841
|
-
request,
|
|
842
|
-
});
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
if (!("uriTemplate" in resource)) {
|
|
846
|
-
throw new UnexpectedStateError("Unexpected resource");
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
if (!resource.complete) {
|
|
850
|
-
throw new UnexpectedStateError(
|
|
851
|
-
"Resource does not support completion",
|
|
852
|
-
{
|
|
853
|
-
request,
|
|
854
|
-
},
|
|
855
|
-
);
|
|
856
|
-
}
|
|
857
|
-
|
|
858
|
-
const completion = CompletionZodSchema.parse(
|
|
859
|
-
await resource.complete(
|
|
860
|
-
request.params.argument.name,
|
|
861
|
-
request.params.argument.value,
|
|
862
|
-
),
|
|
863
|
-
);
|
|
864
|
-
|
|
865
|
-
return {
|
|
866
|
-
completion,
|
|
867
|
-
};
|
|
868
|
-
}
|
|
869
|
-
|
|
870
|
-
throw new UnexpectedStateError("Unexpected completion request", {
|
|
871
|
-
request,
|
|
872
|
-
});
|
|
873
|
-
});
|
|
874
|
-
}
|
|
875
|
-
|
|
876
|
-
private setupRootsHandlers() {
|
|
877
|
-
this.#server.setNotificationHandler(
|
|
878
|
-
RootsListChangedNotificationSchema,
|
|
879
|
-
() => {
|
|
880
|
-
this.#server.listRoots().then((roots) => {
|
|
881
|
-
this.#roots = roots.roots;
|
|
882
|
-
|
|
883
|
-
this.emit("rootsChanged", {
|
|
884
|
-
roots: roots.roots,
|
|
885
|
-
});
|
|
886
|
-
});
|
|
887
|
-
},
|
|
888
|
-
);
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
private setupLoggingHandlers() {
|
|
892
|
-
this.#server.setRequestHandler(SetLevelRequestSchema, (request) => {
|
|
893
|
-
this.#loggingLevel = request.params.level;
|
|
894
|
-
|
|
895
|
-
return {};
|
|
896
|
-
});
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
private setupToolHandlers(tools: Tool<T>[]) {
|
|
900
|
-
this.#server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
901
|
-
return {
|
|
902
|
-
tools: tools.map((tool) => {
|
|
903
|
-
return {
|
|
904
|
-
name: tool.name,
|
|
905
|
-
description: tool.description,
|
|
906
|
-
inputSchema: tool.parameters
|
|
907
|
-
? zodToJsonSchema(tool.parameters)
|
|
908
|
-
: undefined,
|
|
909
|
-
};
|
|
910
|
-
}),
|
|
911
|
-
};
|
|
912
|
-
});
|
|
913
|
-
|
|
914
|
-
this.#server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
915
|
-
const tool = tools.find((tool) => tool.name === request.params.name);
|
|
916
|
-
|
|
917
|
-
if (!tool) {
|
|
918
|
-
throw new McpError(
|
|
919
|
-
ErrorCode.MethodNotFound,
|
|
920
|
-
`Unknown tool: ${request.params.name}`,
|
|
921
|
-
);
|
|
922
|
-
}
|
|
923
|
-
|
|
924
|
-
let args: any = undefined;
|
|
925
|
-
|
|
926
|
-
if (tool.parameters) {
|
|
927
|
-
const parsed = tool.parameters.safeParse(request.params.arguments);
|
|
928
|
-
|
|
929
|
-
if (!parsed.success) {
|
|
930
|
-
throw new McpError(
|
|
931
|
-
ErrorCode.InvalidParams,
|
|
932
|
-
`Invalid ${request.params.name} parameters`,
|
|
933
|
-
);
|
|
934
|
-
}
|
|
935
|
-
|
|
936
|
-
args = parsed.data;
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
const progressToken = request.params?._meta?.progressToken;
|
|
940
|
-
|
|
941
|
-
let result: ContentResult;
|
|
942
|
-
|
|
943
|
-
try {
|
|
944
|
-
const reportProgress = async (progress: Progress) => {
|
|
945
|
-
await this.#server.notification({
|
|
946
|
-
method: "notifications/progress",
|
|
947
|
-
params: {
|
|
948
|
-
...progress,
|
|
949
|
-
progressToken,
|
|
950
|
-
},
|
|
951
|
-
});
|
|
952
|
-
};
|
|
953
|
-
|
|
954
|
-
const log = {
|
|
955
|
-
debug: (message: string, context?: SerializableValue) => {
|
|
956
|
-
this.#server.sendLoggingMessage({
|
|
957
|
-
level: "debug",
|
|
958
|
-
data: {
|
|
959
|
-
message,
|
|
960
|
-
context,
|
|
961
|
-
},
|
|
962
|
-
});
|
|
963
|
-
},
|
|
964
|
-
error: (message: string, context?: SerializableValue) => {
|
|
965
|
-
this.#server.sendLoggingMessage({
|
|
966
|
-
level: "error",
|
|
967
|
-
data: {
|
|
968
|
-
message,
|
|
969
|
-
context,
|
|
970
|
-
},
|
|
971
|
-
});
|
|
972
|
-
},
|
|
973
|
-
info: (message: string, context?: SerializableValue) => {
|
|
974
|
-
this.#server.sendLoggingMessage({
|
|
975
|
-
level: "info",
|
|
976
|
-
data: {
|
|
977
|
-
message,
|
|
978
|
-
context,
|
|
979
|
-
},
|
|
980
|
-
});
|
|
981
|
-
},
|
|
982
|
-
warn: (message: string, context?: SerializableValue) => {
|
|
983
|
-
this.#server.sendLoggingMessage({
|
|
984
|
-
level: "warning",
|
|
985
|
-
data: {
|
|
986
|
-
message,
|
|
987
|
-
context,
|
|
988
|
-
},
|
|
989
|
-
});
|
|
990
|
-
},
|
|
991
|
-
};
|
|
992
|
-
|
|
993
|
-
const maybeStringResult = await tool.execute(args, {
|
|
994
|
-
reportProgress,
|
|
995
|
-
log,
|
|
996
|
-
session: this.#auth,
|
|
997
|
-
});
|
|
998
|
-
|
|
999
|
-
if (typeof maybeStringResult === "string") {
|
|
1000
|
-
result = ContentResultZodSchema.parse({
|
|
1001
|
-
content: [{ type: "text", text: maybeStringResult }],
|
|
1002
|
-
});
|
|
1003
|
-
} else if ("type" in maybeStringResult) {
|
|
1004
|
-
result = ContentResultZodSchema.parse({
|
|
1005
|
-
content: [maybeStringResult],
|
|
1006
|
-
});
|
|
1007
|
-
} else {
|
|
1008
|
-
result = ContentResultZodSchema.parse(maybeStringResult);
|
|
1009
|
-
}
|
|
1010
|
-
} catch (error) {
|
|
1011
|
-
if (error instanceof UserError) {
|
|
1012
|
-
return {
|
|
1013
|
-
content: [{ type: "text", text: error.message }],
|
|
1014
|
-
isError: true,
|
|
1015
|
-
};
|
|
1016
|
-
}
|
|
1017
|
-
|
|
1018
|
-
return {
|
|
1019
|
-
content: [{ type: "text", text: `Error: ${error}` }],
|
|
1020
|
-
isError: true,
|
|
1021
|
-
};
|
|
1022
|
-
}
|
|
1023
|
-
|
|
1024
|
-
return result;
|
|
1025
|
-
});
|
|
1026
|
-
}
|
|
1027
|
-
|
|
1028
|
-
private setupResourceHandlers(resources: Resource[]) {
|
|
1029
|
-
this.#server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
|
1030
|
-
return {
|
|
1031
|
-
resources: resources.map((resource) => {
|
|
1032
|
-
return {
|
|
1033
|
-
uri: resource.uri,
|
|
1034
|
-
name: resource.name,
|
|
1035
|
-
mimeType: resource.mimeType,
|
|
1036
|
-
};
|
|
1037
|
-
}),
|
|
1038
|
-
};
|
|
1039
|
-
});
|
|
1040
|
-
|
|
1041
|
-
this.#server.setRequestHandler(
|
|
1042
|
-
ReadResourceRequestSchema,
|
|
1043
|
-
async (request) => {
|
|
1044
|
-
if ("uri" in request.params) {
|
|
1045
|
-
const resource = resources.find(
|
|
1046
|
-
(resource) =>
|
|
1047
|
-
"uri" in resource && resource.uri === request.params.uri,
|
|
1048
|
-
);
|
|
1049
|
-
|
|
1050
|
-
if (!resource) {
|
|
1051
|
-
for (const resourceTemplate of this.#resourceTemplates) {
|
|
1052
|
-
const uriTemplate = parseURITemplate(
|
|
1053
|
-
resourceTemplate.uriTemplate,
|
|
1054
|
-
);
|
|
1055
|
-
|
|
1056
|
-
const match = uriTemplate.fromUri(request.params.uri);
|
|
1057
|
-
|
|
1058
|
-
if (!match) {
|
|
1059
|
-
continue;
|
|
1060
|
-
}
|
|
1061
|
-
|
|
1062
|
-
const uri = uriTemplate.fill(match);
|
|
1063
|
-
|
|
1064
|
-
const result = await resourceTemplate.load(match);
|
|
1065
|
-
|
|
1066
|
-
return {
|
|
1067
|
-
contents: [
|
|
1068
|
-
{
|
|
1069
|
-
uri: uri,
|
|
1070
|
-
mimeType: resourceTemplate.mimeType,
|
|
1071
|
-
name: resourceTemplate.name,
|
|
1072
|
-
...result,
|
|
1073
|
-
},
|
|
1074
|
-
],
|
|
1075
|
-
};
|
|
1076
|
-
}
|
|
1077
|
-
|
|
1078
|
-
throw new McpError(
|
|
1079
|
-
ErrorCode.MethodNotFound,
|
|
1080
|
-
`Unknown resource: ${request.params.uri}`,
|
|
1081
|
-
);
|
|
1082
|
-
}
|
|
1083
|
-
|
|
1084
|
-
if (!("uri" in resource)) {
|
|
1085
|
-
throw new UnexpectedStateError("Resource does not support reading");
|
|
1086
|
-
}
|
|
1087
|
-
|
|
1088
|
-
let maybeArrayResult: Awaited<ReturnType<Resource["load"]>>;
|
|
1089
|
-
|
|
1090
|
-
try {
|
|
1091
|
-
maybeArrayResult = await resource.load();
|
|
1092
|
-
} catch (error) {
|
|
1093
|
-
throw new McpError(
|
|
1094
|
-
ErrorCode.InternalError,
|
|
1095
|
-
`Error reading resource: ${error}`,
|
|
1096
|
-
{
|
|
1097
|
-
uri: resource.uri,
|
|
1098
|
-
},
|
|
1099
|
-
);
|
|
1100
|
-
}
|
|
1101
|
-
|
|
1102
|
-
if (Array.isArray(maybeArrayResult)) {
|
|
1103
|
-
return {
|
|
1104
|
-
contents: maybeArrayResult.map((result) => ({
|
|
1105
|
-
uri: resource.uri,
|
|
1106
|
-
mimeType: resource.mimeType,
|
|
1107
|
-
name: resource.name,
|
|
1108
|
-
...result,
|
|
1109
|
-
})),
|
|
1110
|
-
};
|
|
1111
|
-
} else {
|
|
1112
|
-
return {
|
|
1113
|
-
contents: [
|
|
1114
|
-
{
|
|
1115
|
-
uri: resource.uri,
|
|
1116
|
-
mimeType: resource.mimeType,
|
|
1117
|
-
name: resource.name,
|
|
1118
|
-
...maybeArrayResult,
|
|
1119
|
-
},
|
|
1120
|
-
],
|
|
1121
|
-
};
|
|
1122
|
-
}
|
|
1123
|
-
}
|
|
1124
|
-
|
|
1125
|
-
throw new UnexpectedStateError("Unknown resource request", {
|
|
1126
|
-
request,
|
|
1127
|
-
});
|
|
1128
|
-
},
|
|
1129
|
-
);
|
|
1130
|
-
}
|
|
1131
|
-
|
|
1132
|
-
private setupResourceTemplateHandlers(resourceTemplates: ResourceTemplate[]) {
|
|
1133
|
-
this.#server.setRequestHandler(
|
|
1134
|
-
ListResourceTemplatesRequestSchema,
|
|
1135
|
-
async () => {
|
|
1136
|
-
return {
|
|
1137
|
-
resourceTemplates: resourceTemplates.map((resourceTemplate) => {
|
|
1138
|
-
return {
|
|
1139
|
-
name: resourceTemplate.name,
|
|
1140
|
-
uriTemplate: resourceTemplate.uriTemplate,
|
|
1141
|
-
};
|
|
1142
|
-
}),
|
|
1143
|
-
};
|
|
1144
|
-
},
|
|
1145
|
-
);
|
|
1146
|
-
}
|
|
1147
|
-
|
|
1148
|
-
private setupPromptHandlers(prompts: Prompt[]) {
|
|
1149
|
-
this.#server.setRequestHandler(ListPromptsRequestSchema, async () => {
|
|
1150
|
-
return {
|
|
1151
|
-
prompts: prompts.map((prompt) => {
|
|
1152
|
-
return {
|
|
1153
|
-
name: prompt.name,
|
|
1154
|
-
description: prompt.description,
|
|
1155
|
-
arguments: prompt.arguments,
|
|
1156
|
-
complete: prompt.complete,
|
|
1157
|
-
};
|
|
1158
|
-
}),
|
|
1159
|
-
};
|
|
1160
|
-
});
|
|
1161
|
-
|
|
1162
|
-
this.#server.setRequestHandler(GetPromptRequestSchema, async (request) => {
|
|
1163
|
-
const prompt = prompts.find(
|
|
1164
|
-
(prompt) => prompt.name === request.params.name,
|
|
1165
|
-
);
|
|
1166
|
-
|
|
1167
|
-
if (!prompt) {
|
|
1168
|
-
throw new McpError(
|
|
1169
|
-
ErrorCode.MethodNotFound,
|
|
1170
|
-
`Unknown prompt: ${request.params.name}`,
|
|
1171
|
-
);
|
|
1172
|
-
}
|
|
1173
|
-
|
|
1174
|
-
const args = request.params.arguments;
|
|
1175
|
-
|
|
1176
|
-
for (const arg of prompt.arguments ?? []) {
|
|
1177
|
-
if (arg.required && !(args && arg.name in args)) {
|
|
1178
|
-
throw new McpError(
|
|
1179
|
-
ErrorCode.InvalidRequest,
|
|
1180
|
-
`Missing required argument: ${arg.name}`,
|
|
1181
|
-
);
|
|
1182
|
-
}
|
|
1183
|
-
}
|
|
1184
|
-
|
|
1185
|
-
let result: Awaited<ReturnType<Prompt["load"]>>;
|
|
1186
|
-
|
|
1187
|
-
try {
|
|
1188
|
-
result = await prompt.load(args as Record<string, string | undefined>);
|
|
1189
|
-
} catch (error) {
|
|
1190
|
-
throw new McpError(
|
|
1191
|
-
ErrorCode.InternalError,
|
|
1192
|
-
`Error loading prompt: ${error}`,
|
|
1193
|
-
);
|
|
1194
|
-
}
|
|
1195
|
-
|
|
1196
|
-
return {
|
|
1197
|
-
description: prompt.description,
|
|
1198
|
-
messages: [
|
|
1199
|
-
{
|
|
1200
|
-
role: "user",
|
|
1201
|
-
content: { type: "text", text: result },
|
|
1202
|
-
},
|
|
1203
|
-
],
|
|
1204
|
-
};
|
|
1205
|
-
});
|
|
1206
|
-
}
|
|
1207
|
-
}
|
|
1208
|
-
|
|
1209
|
-
const FastMCPEventEmitterBase: {
|
|
1210
|
-
new (): StrictEventEmitter<EventEmitter, FastMCPEvents<FastMCPSessionAuth>>;
|
|
1211
|
-
} = EventEmitter;
|
|
1212
|
-
|
|
1213
|
-
class FastMCPEventEmitter extends FastMCPEventEmitterBase {}
|
|
1214
|
-
|
|
1215
|
-
type Authenticate<T> = (request: http.IncomingMessage) => Promise<T>;
|
|
1216
|
-
|
|
1217
|
-
/**
|
|
1218
|
-
* Class representing a toolbox for FastMCP.
|
|
1219
|
-
* Manages tools, resources, and prompts for a Model Context Protocol server.
|
|
1220
|
-
*/
|
|
1221
|
-
export class ToolBox<T extends Record<string, unknown> | undefined = undefined> extends FastMCPEventEmitter {
|
|
1222
|
-
#options: ServerOptions<T>;
|
|
1223
|
-
#prompts: InputPrompt[] = [];
|
|
1224
|
-
#resources: Resource[] = [];
|
|
1225
|
-
#resourcesTemplates: InputResourceTemplate[] = [];
|
|
1226
|
-
#sessions: FastMCPSession<T>[] = [];
|
|
1227
|
-
#sseServer: SSEServer | null = null;
|
|
1228
|
-
#tools: Tool<T>[] = [];
|
|
1229
|
-
#authenticate: Authenticate<T> | undefined;
|
|
1230
|
-
|
|
1231
|
-
/**
|
|
1232
|
-
* Creates a new ToolBox instance.
|
|
1233
|
-
*
|
|
1234
|
-
* @param options - Configuration options for the toolbox
|
|
1235
|
-
*/
|
|
1236
|
-
constructor(public options: ServerOptions<T>) {
|
|
1237
|
-
super();
|
|
1238
|
-
|
|
1239
|
-
this.#options = options;
|
|
1240
|
-
this.#authenticate = options.authenticate;
|
|
1241
|
-
}
|
|
1242
|
-
|
|
1243
|
-
/**
|
|
1244
|
-
* Gets all active sessions.
|
|
1245
|
-
*/
|
|
1246
|
-
public get sessions(): FastMCPSession<T>[] {
|
|
1247
|
-
return this.#sessions;
|
|
1248
|
-
}
|
|
1249
|
-
|
|
1250
|
-
/**
|
|
1251
|
-
* Adds a tool to the server.
|
|
1252
|
-
*
|
|
1253
|
-
* @param tool - The tool to add
|
|
1254
|
-
*/
|
|
1255
|
-
public addTool<Params extends ToolParameters>(tool: Tool<T, Params>) {
|
|
1256
|
-
this.#tools.push(tool as unknown as Tool<T>);
|
|
1257
|
-
}
|
|
1258
|
-
|
|
1259
|
-
/**
|
|
1260
|
-
* Adds a resource to the server.
|
|
1261
|
-
*
|
|
1262
|
-
* @param resource - The resource to add
|
|
1263
|
-
*/
|
|
1264
|
-
public addResource(resource: Resource) {
|
|
1265
|
-
this.#resources.push(resource);
|
|
1266
|
-
}
|
|
1267
|
-
|
|
1268
|
-
/**
|
|
1269
|
-
* Adds a resource template to the server.
|
|
1270
|
-
*
|
|
1271
|
-
* @param resource - The resource template to add
|
|
1272
|
-
*/
|
|
1273
|
-
public addResourceTemplate<
|
|
1274
|
-
const Args extends InputResourceTemplateArgument[],
|
|
1275
|
-
>(resource: InputResourceTemplate<Args>) {
|
|
1276
|
-
this.#resourcesTemplates.push(resource);
|
|
1277
|
-
}
|
|
1278
|
-
|
|
1279
|
-
/**
|
|
1280
|
-
* Adds a prompt to the server.
|
|
1281
|
-
*
|
|
1282
|
-
* @param prompt - The prompt to add
|
|
1283
|
-
*/
|
|
1284
|
-
public addPrompt<const Args extends InputPromptArgument[]>(
|
|
1285
|
-
prompt: InputPrompt<Args>,
|
|
1286
|
-
) {
|
|
1287
|
-
this.#prompts.push(prompt);
|
|
1288
|
-
}
|
|
1289
|
-
|
|
1290
|
-
/**
|
|
1291
|
-
* Starts the server.
|
|
1292
|
-
*
|
|
1293
|
-
* @param options - Options for the server transport
|
|
1294
|
-
*/
|
|
1295
|
-
public async start(
|
|
1296
|
-
options:
|
|
1297
|
-
| { transportType: "stdio" }
|
|
1298
|
-
| {
|
|
1299
|
-
transportType: "sse";
|
|
1300
|
-
sse: { endpoint: `/${string}`; port: number };
|
|
1301
|
-
} = {
|
|
1302
|
-
transportType: "stdio",
|
|
1303
|
-
},
|
|
1304
|
-
) {
|
|
1305
|
-
if (options.transportType === "stdio") {
|
|
1306
|
-
const transport = new StdioServerTransport();
|
|
1307
|
-
|
|
1308
|
-
const session = new FastMCPSession<T>({
|
|
1309
|
-
name: this.#options.name,
|
|
1310
|
-
version: this.#options.version,
|
|
1311
|
-
tools: this.#tools,
|
|
1312
|
-
resources: this.#resources,
|
|
1313
|
-
resourcesTemplates: this.#resourcesTemplates,
|
|
1314
|
-
prompts: this.#prompts,
|
|
1315
|
-
});
|
|
1316
|
-
|
|
1317
|
-
await session.connect(transport);
|
|
1318
|
-
|
|
1319
|
-
this.#sessions.push(session);
|
|
1320
|
-
|
|
1321
|
-
this.emit("connect", {
|
|
1322
|
-
session,
|
|
1323
|
-
});
|
|
1324
|
-
|
|
1325
|
-
} else if (options.transportType === "sse") {
|
|
1326
|
-
|
|
1327
|
-
} else {
|
|
1328
|
-
throw new Error("Invalid transport type");
|
|
1329
|
-
}
|
|
1330
|
-
}
|
|
1331
|
-
|
|
1332
|
-
/**
|
|
1333
|
-
* Stops the server.
|
|
1334
|
-
*/
|
|
1335
|
-
public async stop() {
|
|
1336
|
-
if (this.#sseServer) {
|
|
1337
|
-
this.#sseServer.close();
|
|
1338
|
-
}
|
|
1339
|
-
}
|
|
1340
|
-
|
|
1341
|
-
/**
|
|
1342
|
-
* Activates the server.
|
|
1343
|
-
*
|
|
1344
|
-
* @param options - Options for the server transport
|
|
1345
|
-
*/
|
|
1346
|
-
public async activate(
|
|
1347
|
-
options:
|
|
1348
|
-
| { transportType: "stdio" }
|
|
1349
|
-
| {
|
|
1350
|
-
transportType: "sse";
|
|
1351
|
-
sse: { endpoint: `/${string}`; port: number };
|
|
1352
|
-
} = {
|
|
1353
|
-
transportType: "stdio",
|
|
1354
|
-
},
|
|
1355
|
-
) {
|
|
1356
|
-
if (options.transportType === "stdio") {
|
|
1357
|
-
const transport = new StdioServerTransport();
|
|
1358
|
-
|
|
1359
|
-
const session = new FastMCPSession({
|
|
1360
|
-
name: this.#options.name,
|
|
1361
|
-
version: this.#options.version,
|
|
1362
|
-
tools: this.#tools,
|
|
1363
|
-
resources: this.#resources,
|
|
1364
|
-
resourcesTemplates: this.#resourcesTemplates,
|
|
1365
|
-
prompts: this.#prompts,
|
|
1366
|
-
});
|
|
1367
|
-
|
|
1368
|
-
await session.connect(transport);
|
|
1369
|
-
|
|
1370
|
-
this.#sessions.push(session);
|
|
1371
|
-
|
|
1372
|
-
this.emit("connect", {
|
|
1373
|
-
session,
|
|
1374
|
-
});
|
|
1375
|
-
|
|
1376
|
-
console.info(`server is running on stdio`);
|
|
1377
|
-
} else if (options.transportType === "sse") {
|
|
1378
|
-
// Implementation for SSE transport
|
|
1379
|
-
} else {
|
|
1380
|
-
throw new Error("Invalid transport type");
|
|
1381
|
-
}
|
|
1382
|
-
}
|
|
1383
|
-
}
|
|
1384
|
-
|