Skip to content

Commit a2fc4d8

Browse files
committed
chore: format README with prettier
1 parent 307e536 commit a2fc4d8

File tree

1 file changed

+64
-75
lines changed

1 file changed

+64
-75
lines changed

README.md

Lines changed: 64 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,8 @@ This setup allows you to:
14831483

14841484
### Custom Reconnection Scheduling for Non-Persistent Environments
14851485

1486-
By default, the Streamable HTTP client transport uses `setTimeout` to schedule automatic reconnections after connection failures. However, this approach doesn't work well in non-persistent environments like serverless functions, mobile apps, or desktop applications that may be suspended.
1486+
By default, the Streamable HTTP client transport uses `setTimeout` to schedule automatic reconnections after connection failures. However, this approach doesn't work well in non-persistent environments like serverless functions, mobile apps, or desktop applications that may be
1487+
suspended.
14871488

14881489
The SDK allows you to provide a custom reconnection scheduler to handle these scenarios:
14891490

@@ -1498,9 +1499,9 @@ The SDK allows you to provide a custom reconnection scheduler to handle these sc
14981499

14991500
```typescript
15001501
type ReconnectionScheduler = (
1501-
reconnect: () => void, // Function to call to initiate reconnection
1502-
delay: number, // Suggested delay in milliseconds
1503-
attemptCount: number // Current reconnection attempt count (0-indexed)
1502+
reconnect: () => void, // Function to call to initiate reconnection
1503+
delay: number, // Suggested delay in milliseconds
1504+
attemptCount: number // Current reconnection attempt count (0-indexed)
15041505
) => void;
15051506
```
15061507

@@ -1512,27 +1513,24 @@ import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/
15121513

15131514
// Serverless scheduler: reconnect immediately without setTimeout
15141515
const serverlessScheduler = (reconnect, delay, attemptCount) => {
1515-
// In serverless, timers don't persist across invocations
1516-
// Just reconnect immediately - the function will handle retries on next invocation
1517-
reconnect();
1516+
// In serverless, timers don't persist across invocations
1517+
// Just reconnect immediately - the function will handle retries on next invocation
1518+
reconnect();
15181519
};
15191520

1520-
const transport = new StreamableHTTPClientTransport(
1521-
new URL('https://api.example.com/mcp'),
1522-
{
1521+
const transport = new StreamableHTTPClientTransport(new URL('https://api.example.com/mcp'), {
15231522
reconnectionOptions: {
1524-
initialReconnectionDelay: 1000,
1525-
maxReconnectionDelay: 30000,
1526-
reconnectionDelayGrowFactor: 2,
1527-
maxRetries: 5
1523+
initialReconnectionDelay: 1000,
1524+
maxReconnectionDelay: 30000,
1525+
reconnectionDelayGrowFactor: 2,
1526+
maxRetries: 5
15281527
},
15291528
reconnectionScheduler: serverlessScheduler
1530-
}
1531-
);
1529+
});
15321530

15331531
const client = new Client({
1534-
name: 'serverless-client',
1535-
version: '1.0.0'
1532+
name: 'serverless-client',
1533+
version: '1.0.0'
15361534
});
15371535

15381536
await client.connect(transport);
@@ -1547,24 +1545,21 @@ For true serverless environments where the function may terminate before reconne
15471545
let storedReconnect: (() => void) | undefined;
15481546

15491547
const deferredScheduler = (reconnect, delay, attemptCount) => {
1550-
// Store the reconnect callback instead of calling it
1551-
// In a real app, persist this to a database or queue
1552-
storedReconnect = reconnect;
1553-
console.log(`Reconnection scheduled for next invocation (attempt ${attemptCount})`);
1548+
// Store the reconnect callback instead of calling it
1549+
// In a real app, persist this to a database or queue
1550+
storedReconnect = reconnect;
1551+
console.log(`Reconnection scheduled for next invocation (attempt ${attemptCount})`);
15541552
};
15551553

1556-
const transport = new StreamableHTTPClientTransport(
1557-
new URL('https://api.example.com/mcp'),
1558-
{
1554+
const transport = new StreamableHTTPClientTransport(new URL('https://api.example.com/mcp'), {
15591555
reconnectionScheduler: deferredScheduler
1560-
}
1561-
);
1556+
});
15621557

15631558
// Later, on next function invocation:
15641559
if (storedReconnect) {
1565-
console.log('Triggering stored reconnection');
1566-
storedReconnect();
1567-
storedReconnect = undefined;
1560+
console.log('Triggering stored reconnection');
1561+
storedReconnect();
1562+
storedReconnect = undefined;
15681563
}
15691564
```
15701565

@@ -1573,68 +1568,62 @@ if (storedReconnect) {
15731568
```typescript
15741569
// iOS/Android mobile app using platform-specific background tasks
15751570
const mobileScheduler = (reconnect, delay, attemptCount) => {
1576-
if (attemptCount > 3) {
1577-
console.log('Too many reconnection attempts, giving up');
1578-
return;
1579-
}
1580-
1581-
// Use native background task API (pseudocode)
1582-
BackgroundTaskManager.schedule({
1583-
taskId: 'mcp-reconnect',
1584-
delay: delay,
1585-
callback: () => {
1586-
console.log(`Background reconnection attempt ${attemptCount}`);
1587-
reconnect();
1571+
if (attemptCount > 3) {
1572+
console.log('Too many reconnection attempts, giving up');
1573+
return;
15881574
}
1589-
});
1575+
1576+
// Use native background task API (pseudocode)
1577+
BackgroundTaskManager.schedule({
1578+
taskId: 'mcp-reconnect',
1579+
delay: delay,
1580+
callback: () => {
1581+
console.log(`Background reconnection attempt ${attemptCount}`);
1582+
reconnect();
1583+
}
1584+
});
15901585
};
15911586

1592-
const transport = new StreamableHTTPClientTransport(
1593-
new URL('https://api.example.com/mcp'),
1594-
{
1587+
const transport = new StreamableHTTPClientTransport(new URL('https://api.example.com/mcp'), {
15951588
reconnectionOptions: {
1596-
initialReconnectionDelay: 5000,
1597-
maxReconnectionDelay: 60000,
1598-
reconnectionDelayGrowFactor: 1.5,
1599-
maxRetries: 3
1589+
initialReconnectionDelay: 5000,
1590+
maxReconnectionDelay: 60000,
1591+
reconnectionDelayGrowFactor: 1.5,
1592+
maxRetries: 3
16001593
},
16011594
reconnectionScheduler: mobileScheduler
1602-
}
1603-
);
1595+
});
16041596
```
16051597

16061598
#### Example: Desktop App with Power Management
16071599

16081600
```typescript
16091601
// Desktop app that respects system sleep/wake cycles
16101602
const desktopScheduler = (reconnect, delay, attemptCount) => {
1611-
const timeoutId = setTimeout(() => {
1612-
// Check if system was sleeping
1613-
const actualElapsed = Date.now() - scheduleTime;
1614-
if (actualElapsed > delay * 1.5) {
1615-
console.log('System was likely sleeping, reconnecting immediately');
1616-
reconnect();
1617-
} else {
1618-
reconnect();
1619-
}
1620-
}, delay);
1603+
const timeoutId = setTimeout(() => {
1604+
// Check if system was sleeping
1605+
const actualElapsed = Date.now() - scheduleTime;
1606+
if (actualElapsed > delay * 1.5) {
1607+
console.log('System was likely sleeping, reconnecting immediately');
1608+
reconnect();
1609+
} else {
1610+
reconnect();
1611+
}
1612+
}, delay);
16211613

1622-
const scheduleTime = Date.now();
1614+
const scheduleTime = Date.now();
16231615

1624-
// Handle system wake events (pseudocode)
1625-
powerMonitor.on('resume', () => {
1626-
clearTimeout(timeoutId);
1627-
console.log('System resumed, reconnecting immediately');
1628-
reconnect();
1629-
});
1616+
// Handle system wake events (pseudocode)
1617+
powerMonitor.on('resume', () => {
1618+
clearTimeout(timeoutId);
1619+
console.log('System resumed, reconnecting immediately');
1620+
reconnect();
1621+
});
16301622
};
16311623

1632-
const transport = new StreamableHTTPClientTransport(
1633-
new URL('https://api.example.com/mcp'),
1634-
{
1624+
const transport = new StreamableHTTPClientTransport(new URL('https://api.example.com/mcp'), {
16351625
reconnectionScheduler: desktopScheduler
1636-
}
1637-
);
1626+
});
16381627
```
16391628

16401629
#### Default Behavior
@@ -1644,7 +1633,7 @@ If no custom scheduler is provided, the transport uses the default `setTimeout`-
16441633
```typescript
16451634
// Default scheduler (built-in)
16461635
const defaultScheduler = (reconnect, delay, attemptCount) => {
1647-
setTimeout(reconnect, delay);
1636+
setTimeout(reconnect, delay);
16481637
};
16491638
```
16501639

0 commit comments

Comments
 (0)