Skip to content

Commit 20300dd

Browse files
committed
feat: expose underlying MQTT client through getMqttClient() method
- Add getMqttClient() method to both McpMqttServer and McpMqttClient - Add getClient() method to UniversalMqttAdapter and MqttAdapter interface - Update README with documentation for accessing underlying MQTT client - Allows users to perform custom pub/sub operations using the same MQTT connection
1 parent 577459d commit 20300dd

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,18 @@ host: 'mqtts://broker.emqx.io:8883'
439439

440440
## Advanced Usage
441441

442+
### Accessing Underlying MQTT Client
443+
444+
Both server and client provide `getMqttClient()` method to access the underlying MQTT client for custom pub/sub operations:
445+
446+
```typescript
447+
const mqttClient = server.getMqttClient() // or client.getMqttClient()
448+
if (mqttClient) {
449+
mqttClient.subscribe('custom/topic', { qos: 1 })
450+
mqttClient.publish('custom/topic', 'Hello World')
451+
}
452+
```
453+
442454
### Custom Tool Validation
443455

444456
```typescript

src/client/mcp-client.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { EventEmitter } from 'events'
2+
import type { MqttClient } from 'mqtt'
23
import type {
34
McpMqttClientConfig,
45
MqttConnectionOptions,
@@ -500,6 +501,10 @@ export class McpMqttClient extends EventEmitter {
500501
isServerConnected(serverId: string): boolean {
501502
return this.connectedServers.has(serverId)
502503
}
504+
505+
getMqttClient(): MqttClient | null {
506+
return this.mqttAdapter.getClient()
507+
}
503508
}
504509

505510
export function createMcpClient(config: McpMqttClientConfig): McpMqttClient {

src/server/mcp-server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,10 @@ export class McpMqttServer extends EventEmitter {
512512
getConnectedClients(): string[] {
513513
return Array.from(this.connectedClients)
514514
}
515+
516+
getMqttClient() {
517+
return this.mqttAdapter.getClient()
518+
}
515519
}
516520

517521
export function createMcpServer(config: McpMqttServerConfig): McpMqttServer {

src/shared/mqtt-adapter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface MqttAdapter {
3030
on(event: 'connect' | 'disconnect' | 'error', callback: (...args: any[]) => void): void
3131
isConnected(): boolean
3232
getConnackProperties(): Record<string, any> | undefined
33+
getClient(): MqttClient | null
3334
}
3435

3536
export class UniversalMqttAdapter implements MqttAdapter {
@@ -209,4 +210,8 @@ export class UniversalMqttAdapter implements MqttAdapter {
209210
getConnackProperties(): Record<string, any> | undefined {
210211
return this.connackProperties
211212
}
213+
214+
getClient(): MqttClient | null {
215+
return this.client
216+
}
212217
}

0 commit comments

Comments
 (0)