MCPcopy
hub / github.com/serverless/serverless / connect

Method connect

packages/serverless/lib/plugins/aws/dev/index.js:852–1117  ·  view source on GitHub ↗

* Connects to the IoT endpoint over websockets and listens for lambda events. * The method subscribes to all function invocation topics and listens for incoming events. * When an event is received, the method invokes the corresponding function locally, waits for the result, * and publishes

()

Source from the content-addressed store, hash-verified

850 * @returns {Promise<void>} This is a long-running method, so it does not return a value.
851 */
852 async connect() {
853 const mainProgress = progress.get('main')
854 logger.debug('Connecting to IoT endpoint')
855
856 const endpoint = await this.getIotEndpoint()
857
858 const {
859 accessKeyId,
860 secretAccessKey: secretKey,
861 sessionToken,
862 } = await this.provider.getCredentials()
863
864 const device = new iot.device({
865 protocol: 'wss',
866 host: endpoint,
867 accessKeyId,
868 secretKey,
869 sessionToken,
870 autoResubscribe: true,
871 offlineQueueing: true,
872 baseReconnectTimeMs: 1000,
873 maximumReconnectTimeMs: 1000,
874 minimumConnectionTimeMs: 1000,
875 keepalive: 1,
876 })
877
878 device.on('error', (e) => {
879 logger.debug('IoT connection error', e)
880 })
881
882 device.on('offline', (e) => {
883 mainProgress.notice('Reconnecting')
884 })
885
886 device.on('connect', (e) => {
887 if (!this.heartbeatInterval) {
888 this.heartbeatInterval = setInterval(() => {
889 device.publish(
890 this.getTopicId('_heartbeat'),
891 JSON.stringify({ connected: true }),
892 {
893 qos: 1,
894 },
895 )
896 }, 1000)
897 }
898
899 logger.success(`Connected (Ctrl+C to cancel)`)
900 mainProgress.remove()
901 })
902
903 // Each function has a seperate topic we need to subscribe to
904 const functionNames = this.serverless.service.getAllFunctions()
905
906 for (const functionName of functionNames) {
907 device.subscribe(this.getTopicId(`${functionName}/request`), {
908 qos: 1,
909 })

Callers 10

devMethod · 0.95
createMcpSessionFunction · 0.80
loadGatewayToolsFunction · 0.80
loadKnowledgeMcpToolsFunction · 0.80
index.jsFile · 0.80
createPrivateMcpSessionFunction · 0.80
createPublicMcpSessionFunction · 0.80
startSseServerFunction · 0.80
startStdioServerFunction · 0.80

Calls 15

getIotEndpointMethod · 0.95
getTopicIdMethod · 0.95
parseTopicIdMethod · 0.95
logFunctionEventMethod · 0.95
invokeMethod · 0.95
logFunctionResponseMethod · 0.95
stringToSafeColorFunction · 0.90
debugMethod · 0.80
getCredentialsMethod · 0.80
publishMethod · 0.80
successMethod · 0.80
getAllFunctionsMethod · 0.80

Tested by

no test coverage detected