@egain/egain-mcp-server 1.0.6 → 1.0.12
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 +22 -0
- package/bin/mcp-server.js +201 -171
- package/bin/mcp-server.js.map +9 -9
- package/esm/src/funcs/getPortals.d.ts +48 -7
- package/esm/src/funcs/getPortals.d.ts.map +1 -1
- package/esm/src/funcs/getPortals.js +48 -7
- package/esm/src/funcs/getPortals.js.map +1 -1
- package/esm/src/hooks/auth-hook.d.ts +6 -1
- package/esm/src/hooks/auth-hook.d.ts.map +1 -1
- package/esm/src/hooks/auth-hook.js +189 -197
- package/esm/src/hooks/auth-hook.js.map +1 -1
- package/esm/src/hooks/tooltip-images.d.ts +10 -0
- package/esm/src/hooks/tooltip-images.d.ts.map +1 -0
- package/esm/src/hooks/tooltip-images.js +12 -0
- package/esm/src/hooks/tooltip-images.js.map +1 -0
- package/esm/src/lib/config.d.ts +2 -2
- package/esm/src/lib/config.js +2 -2
- package/esm/src/lib/config.js.map +1 -1
- package/esm/src/mcp-server/mcp-server.js +1 -1
- package/esm/src/mcp-server/mcp-server.js.map +1 -1
- package/esm/src/mcp-server/server.js +1 -1
- package/esm/src/mcp-server/server.js.map +1 -1
- package/esm/src/mcp-server/tools/getPortals.d.ts.map +1 -1
- package/esm/src/mcp-server/tools/getPortals.js +48 -7
- package/esm/src/mcp-server/tools/getPortals.js.map +1 -1
- package/esm/src/models/getmyportalsop.d.ts +1 -1
- package/esm/src/models/getmyportalsop.d.ts.map +1 -1
- package/manifest.json +2 -2
- package/package.json +1 -1
- package/src/funcs/getPortals.ts +48 -7
- package/src/hooks/auth-hook.ts +200 -224
- package/src/lib/config.ts +2 -2
- package/src/mcp-server/mcp-server.ts +1 -1
- package/src/mcp-server/server.ts +1 -1
- package/src/mcp-server/tools/getPortals.ts +48 -7
- package/src/models/getmyportalsop.ts +1 -1
package/src/hooks/auth-hook.ts
CHANGED
|
@@ -1738,170 +1738,6 @@ export class AuthenticationHook implements SDKInitHook, BeforeRequestHook {
|
|
|
1738
1738
|
* Monitor browser window for authorization code in URL
|
|
1739
1739
|
* Works with ANY redirect URL - detects when URL contains code= parameter
|
|
1740
1740
|
*/
|
|
1741
|
-
private async monitorBrowserForAuthCode(): Promise<string> {
|
|
1742
|
-
const platform = process.platform;
|
|
1743
|
-
const timeout = 120; // 2 minutes
|
|
1744
|
-
const startTime = Date.now();
|
|
1745
|
-
|
|
1746
|
-
if (platform === 'darwin') {
|
|
1747
|
-
// macOS - Monitor using AppleScript
|
|
1748
|
-
console.error(`🔍 Monitoring ${this.detectedBrowser} for authorization code...`);
|
|
1749
|
-
let lastUrl = '';
|
|
1750
|
-
|
|
1751
|
-
while ((Date.now() - startTime) < timeout * 1000) {
|
|
1752
|
-
try {
|
|
1753
|
-
// Get URL from browser using AppleScript
|
|
1754
|
-
const script = `
|
|
1755
|
-
tell application "${this.detectedBrowser}"
|
|
1756
|
-
try
|
|
1757
|
-
set currentURL to URL of active tab of front window
|
|
1758
|
-
return currentURL
|
|
1759
|
-
on error
|
|
1760
|
-
return ""
|
|
1761
|
-
end try
|
|
1762
|
-
end tell
|
|
1763
|
-
`;
|
|
1764
|
-
|
|
1765
|
-
const { stdout } = await execAsync(`osascript -e '${script}'`);
|
|
1766
|
-
const currentUrl = stdout.trim();
|
|
1767
|
-
|
|
1768
|
-
if (currentUrl && currentUrl !== lastUrl) {
|
|
1769
|
-
lastUrl = currentUrl;
|
|
1770
|
-
console.error(`🔍 Current URL: ${currentUrl}`);
|
|
1771
|
-
}
|
|
1772
|
-
|
|
1773
|
-
// Check if URL contains code= parameter (regardless of domain)
|
|
1774
|
-
if (currentUrl && currentUrl.includes('code=')) {
|
|
1775
|
-
console.error('✅ Found authorization code in URL!');
|
|
1776
|
-
|
|
1777
|
-
// Extract the code from the URL
|
|
1778
|
-
const codeMatch = currentUrl.match(/[?&]code=([^&]+)/);
|
|
1779
|
-
if (codeMatch && codeMatch[1]) {
|
|
1780
|
-
const code = decodeURIComponent(codeMatch[1]);
|
|
1781
|
-
console.error(`🔑 Extracted authorization code (first 20 chars): ${code.substring(0, 20)}...`);
|
|
1782
|
-
console.error(` Code length: ${code.length} characters`);
|
|
1783
|
-
|
|
1784
|
-
// Close the browser window immediately (non-blocking - fire and forget)
|
|
1785
|
-
setImmediate(async () => {
|
|
1786
|
-
try {
|
|
1787
|
-
await execAsync(`osascript -e 'tell application "${this.detectedBrowser}" to close front window'`);
|
|
1788
|
-
console.error('✅ Browser window closed');
|
|
1789
|
-
} catch (closeError) {
|
|
1790
|
-
console.error('⚠️ Could not close browser window:', closeError);
|
|
1791
|
-
}
|
|
1792
|
-
});
|
|
1793
|
-
|
|
1794
|
-
// Return code immediately without waiting for window close
|
|
1795
|
-
return code;
|
|
1796
|
-
}
|
|
1797
|
-
}
|
|
1798
|
-
|
|
1799
|
-
// Also check for error parameters
|
|
1800
|
-
if (currentUrl && currentUrl.includes('error=')) {
|
|
1801
|
-
const errorMatch = currentUrl.match(/[?&]error=([^&]+)/);
|
|
1802
|
-
const errorDescMatch = currentUrl.match(/error_description=([^&]+)/);
|
|
1803
|
-
const error = errorMatch && errorMatch[1] ? decodeURIComponent(errorMatch[1]) : 'unknown_error';
|
|
1804
|
-
const errorDesc = errorDescMatch && errorDescMatch[1] ? decodeURIComponent(errorDescMatch[1]) : 'No description';
|
|
1805
|
-
|
|
1806
|
-
// Throw OAuth error - this will stop monitoring but window stays open so user can see the error
|
|
1807
|
-
throw new Error(`OAuth error: ${error} - ${errorDesc}`);
|
|
1808
|
-
}
|
|
1809
|
-
|
|
1810
|
-
} catch (error) {
|
|
1811
|
-
// Re-throw OAuth errors (they should stop monitoring but window stays open)
|
|
1812
|
-
if (error instanceof Error && error.message.includes('OAuth error:')) {
|
|
1813
|
-
throw error;
|
|
1814
|
-
}
|
|
1815
|
-
// Ignore AppleScript errors and continue monitoring
|
|
1816
|
-
}
|
|
1817
|
-
|
|
1818
|
-
// Wait 500ms before checking again
|
|
1819
|
-
await new Promise(resolve => setTimeout(resolve, 500));
|
|
1820
|
-
}
|
|
1821
|
-
|
|
1822
|
-
throw new Error('Authentication timeout. Please try again.');
|
|
1823
|
-
|
|
1824
|
-
} else if (platform === 'win32') {
|
|
1825
|
-
// Windows - Monitor browser window title (contains URL in most browsers)
|
|
1826
|
-
console.error(`🔍 Monitoring ${this.detectedBrowser} for authorization code...`);
|
|
1827
|
-
let lastTitle = '';
|
|
1828
|
-
|
|
1829
|
-
while ((Date.now() - startTime) < timeout * 1000) {
|
|
1830
|
-
try {
|
|
1831
|
-
// PowerShell script to get browser window title
|
|
1832
|
-
// Window titles often contain the URL or page title
|
|
1833
|
-
const browserProcessName = this.detectedBrowser.replace('.exe', '');
|
|
1834
|
-
const psScript = `
|
|
1835
|
-
$process = Get-Process -Name "${browserProcessName}" -ErrorAction SilentlyContinue |
|
|
1836
|
-
Where-Object { $_.MainWindowHandle -ne 0 } |
|
|
1837
|
-
Select-Object -First 1
|
|
1838
|
-
if ($process) {
|
|
1839
|
-
$process.MainWindowTitle
|
|
1840
|
-
}
|
|
1841
|
-
`.replace(/\n\s+/g, ' ');
|
|
1842
|
-
|
|
1843
|
-
const { stdout } = await execAsync(`powershell -Command "${psScript}"`);
|
|
1844
|
-
const windowTitle = stdout.trim();
|
|
1845
|
-
|
|
1846
|
-
if (windowTitle && windowTitle !== lastTitle) {
|
|
1847
|
-
lastTitle = windowTitle;
|
|
1848
|
-
console.error(`🔍 Browser window: ${windowTitle.substring(0, 100)}...`);
|
|
1849
|
-
|
|
1850
|
-
// Check if title or URL contains the code parameter
|
|
1851
|
-
// Most browsers show URL in the title or we can detect redirect completion
|
|
1852
|
-
if (windowTitle.includes('code=') || windowTitle.includes('localhost:3333')) {
|
|
1853
|
-
console.error('✅ Detected OAuth callback!');
|
|
1854
|
-
|
|
1855
|
-
// Try to extract code from title if visible
|
|
1856
|
-
const codeMatch = windowTitle.match(/code=([^&\s]+)/);
|
|
1857
|
-
if (codeMatch && codeMatch[1]) {
|
|
1858
|
-
const code = decodeURIComponent(codeMatch[1]);
|
|
1859
|
-
console.error(`🔑 Extracted authorization code (first 20 chars): ${code.substring(0, 20)}...`);
|
|
1860
|
-
console.error(` Code length: ${code.length} characters`);
|
|
1861
|
-
|
|
1862
|
-
// Close browser window immediately (non-blocking - fire and forget)
|
|
1863
|
-
setImmediate(async () => {
|
|
1864
|
-
try {
|
|
1865
|
-
await execAsync(`powershell -Command "Stop-Process -Name '${browserProcessName}' -Force"`);
|
|
1866
|
-
console.error('✅ Browser window closed');
|
|
1867
|
-
} catch (closeError) {
|
|
1868
|
-
console.error('⚠️ Could not close browser window:', closeError);
|
|
1869
|
-
}
|
|
1870
|
-
});
|
|
1871
|
-
|
|
1872
|
-
// Return code immediately without waiting for window close
|
|
1873
|
-
return code;
|
|
1874
|
-
}
|
|
1875
|
-
}
|
|
1876
|
-
|
|
1877
|
-
// Check for OAuth error in title
|
|
1878
|
-
if (windowTitle.includes('error=')) {
|
|
1879
|
-
const errorMatch = windowTitle.match(/error=([^&\s]+)/);
|
|
1880
|
-
const error = errorMatch && errorMatch[1] ? decodeURIComponent(errorMatch[1]) : 'unknown_error';
|
|
1881
|
-
// Throw OAuth error - this will stop monitoring but window stays open so user can see the error
|
|
1882
|
-
throw new Error(`OAuth error: ${error}`);
|
|
1883
|
-
}
|
|
1884
|
-
}
|
|
1885
|
-
|
|
1886
|
-
} catch (error) {
|
|
1887
|
-
// Re-throw OAuth errors (they should stop monitoring but window stays open)
|
|
1888
|
-
if (error instanceof Error && error.message.includes('OAuth error:')) {
|
|
1889
|
-
throw error;
|
|
1890
|
-
}
|
|
1891
|
-
// Ignore other errors and continue monitoring
|
|
1892
|
-
}
|
|
1893
|
-
|
|
1894
|
-
// Wait 500ms before checking again
|
|
1895
|
-
await new Promise(resolve => setTimeout(resolve, 500));
|
|
1896
|
-
}
|
|
1897
|
-
|
|
1898
|
-
throw new Error('Authentication timeout. The browser window title did not show the authorization code. Please ensure your redirect URL is http://localhost:3333/callback for automatic detection on Windows.');
|
|
1899
|
-
|
|
1900
|
-
} else {
|
|
1901
|
-
throw new Error('Linux is not supported. Use macOS or Windows for automatic authentication.');
|
|
1902
|
-
}
|
|
1903
|
-
}
|
|
1904
|
-
|
|
1905
1741
|
private async getUserAccessToken(code: string): Promise<string> {
|
|
1906
1742
|
const { clientId, clientSecret, redirectUri, accessUrl } = this.authConfig;
|
|
1907
1743
|
|
|
@@ -2078,6 +1914,26 @@ export class AuthenticationHook implements SDKInitHook, BeforeRequestHook {
|
|
|
2078
1914
|
return true;
|
|
2079
1915
|
} else {
|
|
2080
1916
|
console.error(`⏰ AUTH: Token expires in ${Math.round(timeUntilExpiry / 1000)} seconds - treating as expired`);
|
|
1917
|
+
// Delete expired token files to prevent reuse
|
|
1918
|
+
const projectRoot = getProjectRoot();
|
|
1919
|
+
const tokenPath = path.join(projectRoot, '.bearer_token');
|
|
1920
|
+
|
|
1921
|
+
try {
|
|
1922
|
+
if (fs.existsSync(tokenPath)) {
|
|
1923
|
+
fs.unlinkSync(tokenPath);
|
|
1924
|
+
console.error('🗑️ Deleted expired bearer token file');
|
|
1925
|
+
}
|
|
1926
|
+
if (fs.existsSync(metadataPath)) {
|
|
1927
|
+
fs.unlinkSync(metadataPath);
|
|
1928
|
+
console.error('🗑️ Deleted expired bearer token metadata file');
|
|
1929
|
+
}
|
|
1930
|
+
} catch (error) {
|
|
1931
|
+
console.error('⚠️ Failed to delete expired token files:', error);
|
|
1932
|
+
}
|
|
1933
|
+
|
|
1934
|
+
// Clear in-memory token to prevent reuse
|
|
1935
|
+
this.token = null;
|
|
1936
|
+
|
|
2081
1937
|
return false;
|
|
2082
1938
|
}
|
|
2083
1939
|
}
|
|
@@ -2102,9 +1958,19 @@ export class AuthenticationHook implements SDKInitHook, BeforeRequestHook {
|
|
|
2102
1958
|
console.error(`🔑 Found existing bearer token at: ${tokenPath}`);
|
|
2103
1959
|
return token;
|
|
2104
1960
|
}
|
|
1961
|
+
} else {
|
|
1962
|
+
// Token file doesn't exist, clear in-memory token if it was set
|
|
1963
|
+
if (this.token) {
|
|
1964
|
+
console.error('🗑️ Token file deleted, clearing in-memory token');
|
|
1965
|
+
this.token = null;
|
|
1966
|
+
}
|
|
2105
1967
|
}
|
|
2106
1968
|
} catch (error) {
|
|
2107
1969
|
console.error('Could not load existing token:', error);
|
|
1970
|
+
// Clear in-memory token on error as well
|
|
1971
|
+
if (this.token) {
|
|
1972
|
+
this.token = null;
|
|
1973
|
+
}
|
|
2108
1974
|
}
|
|
2109
1975
|
return null;
|
|
2110
1976
|
}
|
|
@@ -2249,34 +2115,7 @@ export class AuthenticationHook implements SDKInitHook, BeforeRequestHook {
|
|
|
2249
2115
|
// Start monitoring browser in background (for ANY redirect URL)
|
|
2250
2116
|
console.error('🔍 Starting browser URL monitoring for authorization code...');
|
|
2251
2117
|
setImmediate(async () => {
|
|
2252
|
-
|
|
2253
|
-
const code = await this.monitorBrowserForAuthCode();
|
|
2254
|
-
console.error('✅ Authorization code detected:', code.substring(0, 10) + '...');
|
|
2255
|
-
|
|
2256
|
-
const accessToken = await this.getUserAccessToken(code);
|
|
2257
|
-
console.error('✅ Access token received');
|
|
2258
|
-
|
|
2259
|
-
this.token = accessToken;
|
|
2260
|
-
|
|
2261
|
-
// Trigger cache initialization if available
|
|
2262
|
-
if (this.portalCacheHook) {
|
|
2263
|
-
try {
|
|
2264
|
-
const fakeRequest = new Request(this.authConfig.environmentUrl!, {
|
|
2265
|
-
headers: { 'Authorization': `Bearer ${accessToken}` }
|
|
2266
|
-
});
|
|
2267
|
-
await this.portalCacheHook.ensureCacheInitialized(fakeRequest);
|
|
2268
|
-
} catch (error) {
|
|
2269
|
-
// Cache init failure is non-fatal
|
|
2270
|
-
}
|
|
2271
|
-
}
|
|
2272
|
-
|
|
2273
|
-
console.error('🎉 Authentication complete! Stopping config server...');
|
|
2274
|
-
this.stopConfigServer();
|
|
2275
|
-
|
|
2276
|
-
} catch (authError: any) {
|
|
2277
|
-
console.error('❌ Authentication monitoring error:', authError);
|
|
2278
|
-
this.stopConfigServer();
|
|
2279
|
-
}
|
|
2118
|
+
await this.monitorBrowserWithRetry();
|
|
2280
2119
|
});
|
|
2281
2120
|
|
|
2282
2121
|
} catch (error: any) {
|
|
@@ -2375,36 +2214,7 @@ export class AuthenticationHook implements SDKInitHook, BeforeRequestHook {
|
|
|
2375
2214
|
// Start monitoring browser in background (for ANY redirect URL)
|
|
2376
2215
|
console.error('🔍 Starting browser URL monitoring for authorization code...');
|
|
2377
2216
|
setImmediate(async () => {
|
|
2378
|
-
|
|
2379
|
-
const code = await this.monitorBrowserForAuthCode();
|
|
2380
|
-
console.error('✅ Authorization code detected:', code.substring(0, 10) + '...');
|
|
2381
|
-
|
|
2382
|
-
const accessToken = await this.getUserAccessToken(code);
|
|
2383
|
-
console.error('✅ Access token received');
|
|
2384
|
-
|
|
2385
|
-
this.token = accessToken;
|
|
2386
|
-
|
|
2387
|
-
// Trigger cache initialization if available
|
|
2388
|
-
if (this.portalCacheHook) {
|
|
2389
|
-
console.error('🔄 Triggering cache initialization...');
|
|
2390
|
-
try {
|
|
2391
|
-
const fakeRequest = new Request(this.authConfig.environmentUrl!, {
|
|
2392
|
-
headers: { 'Authorization': `Bearer ${accessToken}` }
|
|
2393
|
-
});
|
|
2394
|
-
await this.portalCacheHook.ensureCacheInitialized(fakeRequest);
|
|
2395
|
-
console.error('✅ Cache initialization completed');
|
|
2396
|
-
} catch (error) {
|
|
2397
|
-
console.error('⚠️ Cache initialization failed:', error);
|
|
2398
|
-
}
|
|
2399
|
-
}
|
|
2400
|
-
|
|
2401
|
-
console.error('🎉 Authentication complete! Stopping config server...');
|
|
2402
|
-
this.stopConfigServer();
|
|
2403
|
-
|
|
2404
|
-
} catch (authError: any) {
|
|
2405
|
-
console.error('❌ Authentication monitoring error:', authError);
|
|
2406
|
-
this.stopConfigServer();
|
|
2407
|
-
}
|
|
2217
|
+
await this.monitorBrowserWithRetry();
|
|
2408
2218
|
});
|
|
2409
2219
|
|
|
2410
2220
|
} catch (error: any) {
|
|
@@ -2477,8 +2287,18 @@ export class AuthenticationHook implements SDKInitHook, BeforeRequestHook {
|
|
|
2477
2287
|
this.stopConfigServer();
|
|
2478
2288
|
|
|
2479
2289
|
} catch (authError: any) {
|
|
2480
|
-
|
|
2481
|
-
|
|
2290
|
+
// Check if this is an OAuth error (like wrong username/password)
|
|
2291
|
+
const isOAuthError = authError instanceof Error && authError.message.includes('OAuth error:');
|
|
2292
|
+
|
|
2293
|
+
if (isOAuthError) {
|
|
2294
|
+
// For OAuth errors, don't stop the server - allow user to try again
|
|
2295
|
+
console.error('❌ OAuth authentication error:', authError.message);
|
|
2296
|
+
console.error('💡 The configuration server will remain running. Please try again with correct credentials.');
|
|
2297
|
+
} else {
|
|
2298
|
+
// For other token exchange errors, stop the server
|
|
2299
|
+
console.error('❌ Token exchange error:', authError);
|
|
2300
|
+
this.stopConfigServer();
|
|
2301
|
+
}
|
|
2482
2302
|
}
|
|
2483
2303
|
});
|
|
2484
2304
|
|
|
@@ -2557,6 +2377,160 @@ export class AuthenticationHook implements SDKInitHook, BeforeRequestHook {
|
|
|
2557
2377
|
});
|
|
2558
2378
|
}
|
|
2559
2379
|
|
|
2380
|
+
/**
|
|
2381
|
+
* Monitor browser for authorization code with retry on OAuth errors
|
|
2382
|
+
* This method will continue monitoring even after OAuth errors (like wrong password)
|
|
2383
|
+
* to allow users to retry authentication
|
|
2384
|
+
*/
|
|
2385
|
+
private async monitorBrowserWithRetry(): Promise<void> {
|
|
2386
|
+
const platform = process.platform;
|
|
2387
|
+
const timeout = 120; // 2 minutes
|
|
2388
|
+
const startTime = Date.now();
|
|
2389
|
+
let oAuthErrorLogged = false;
|
|
2390
|
+
let lastUrl = '';
|
|
2391
|
+
let lastErrorUrl: string | null = null;
|
|
2392
|
+
|
|
2393
|
+
// Log monitoring start only once
|
|
2394
|
+
if (platform === 'darwin') {
|
|
2395
|
+
console.error(`🔍 Monitoring ${this.detectedBrowser} for authorization code...`);
|
|
2396
|
+
} else if (platform === 'win32') {
|
|
2397
|
+
console.error(`🔍 Monitoring ${this.detectedBrowser} for authorization code...`);
|
|
2398
|
+
}
|
|
2399
|
+
|
|
2400
|
+
while (true) {
|
|
2401
|
+
try {
|
|
2402
|
+
// Check timeout
|
|
2403
|
+
if ((Date.now() - startTime) >= timeout * 1000) {
|
|
2404
|
+
throw new Error('Authentication timeout. Please try again.');
|
|
2405
|
+
}
|
|
2406
|
+
|
|
2407
|
+
let currentUrl = '';
|
|
2408
|
+
|
|
2409
|
+
if (platform === 'darwin') {
|
|
2410
|
+
// macOS - Get URL from browser using AppleScript
|
|
2411
|
+
const script = `
|
|
2412
|
+
tell application "${this.detectedBrowser}"
|
|
2413
|
+
try
|
|
2414
|
+
set currentURL to URL of active tab of front window
|
|
2415
|
+
return currentURL
|
|
2416
|
+
on error
|
|
2417
|
+
return ""
|
|
2418
|
+
end try
|
|
2419
|
+
end tell
|
|
2420
|
+
`;
|
|
2421
|
+
const { stdout } = await execAsync(`osascript -e '${script}'`);
|
|
2422
|
+
currentUrl = stdout.trim();
|
|
2423
|
+
} else if (platform === 'win32') {
|
|
2424
|
+
// Windows - Get browser window title
|
|
2425
|
+
const browserProcessName = this.detectedBrowser.replace('.exe', '');
|
|
2426
|
+
const psScript = `
|
|
2427
|
+
$process = Get-Process -Name "${browserProcessName}" -ErrorAction SilentlyContinue |
|
|
2428
|
+
Where-Object { $_.MainWindowHandle -ne 0 } |
|
|
2429
|
+
Select-Object -First 1
|
|
2430
|
+
if ($process) {
|
|
2431
|
+
$process.MainWindowTitle
|
|
2432
|
+
}
|
|
2433
|
+
`.replace(/\n\s+/g, ' ');
|
|
2434
|
+
const { stdout } = await execAsync(`powershell -Command "${psScript}"`);
|
|
2435
|
+
currentUrl = stdout.trim();
|
|
2436
|
+
}
|
|
2437
|
+
|
|
2438
|
+
// Only log URL when it changes
|
|
2439
|
+
if (currentUrl && currentUrl !== lastUrl) {
|
|
2440
|
+
lastUrl = currentUrl;
|
|
2441
|
+
console.error(`🔍 Current URL: ${currentUrl}`);
|
|
2442
|
+
}
|
|
2443
|
+
|
|
2444
|
+
// Check if URL contains code= parameter
|
|
2445
|
+
if (currentUrl && currentUrl.includes('code=')) {
|
|
2446
|
+
const codeMatch = currentUrl.match(/[?&]code=([^&]+)/);
|
|
2447
|
+
if (codeMatch && codeMatch[1]) {
|
|
2448
|
+
const code = decodeURIComponent(codeMatch[1]);
|
|
2449
|
+
console.error('✅ Found authorization code in URL!');
|
|
2450
|
+
console.error(`🔑 Extracted authorization code (first 20 chars): ${code.substring(0, 20)}...`);
|
|
2451
|
+
|
|
2452
|
+
// Close browser window (non-blocking)
|
|
2453
|
+
setImmediate(async () => {
|
|
2454
|
+
try {
|
|
2455
|
+
if (platform === 'darwin') {
|
|
2456
|
+
await execAsync(`osascript -e 'tell application "${this.detectedBrowser}" to close front window'`);
|
|
2457
|
+
} else if (platform === 'win32') {
|
|
2458
|
+
const browserProcessName = this.detectedBrowser.replace('.exe', '');
|
|
2459
|
+
await execAsync(`powershell -Command "Stop-Process -Name '${browserProcessName}' -Force"`);
|
|
2460
|
+
}
|
|
2461
|
+
} catch (closeError) {
|
|
2462
|
+
// Ignore close errors
|
|
2463
|
+
}
|
|
2464
|
+
});
|
|
2465
|
+
|
|
2466
|
+
console.error('✅ Authorization code detected:', code.substring(0, 10) + '...');
|
|
2467
|
+
|
|
2468
|
+
const accessToken = await this.getUserAccessToken(code);
|
|
2469
|
+
console.error('✅ Access token received');
|
|
2470
|
+
|
|
2471
|
+
this.token = accessToken;
|
|
2472
|
+
|
|
2473
|
+
// Trigger cache initialization if available
|
|
2474
|
+
if (this.portalCacheHook) {
|
|
2475
|
+
try {
|
|
2476
|
+
const fakeRequest = new Request(this.authConfig.environmentUrl!, {
|
|
2477
|
+
headers: { 'Authorization': `Bearer ${accessToken}` }
|
|
2478
|
+
});
|
|
2479
|
+
await this.portalCacheHook.ensureCacheInitialized(fakeRequest);
|
|
2480
|
+
console.error('✅ Cache initialization completed');
|
|
2481
|
+
} catch (error) {
|
|
2482
|
+
console.error('⚠️ Cache initialization failed:', error);
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
|
|
2486
|
+
console.error('🎉 Authentication complete! Stopping config server...');
|
|
2487
|
+
this.stopConfigServer();
|
|
2488
|
+
return; // Success - exit the loop
|
|
2489
|
+
}
|
|
2490
|
+
}
|
|
2491
|
+
|
|
2492
|
+
// Check for error parameters - only throw if this is a new error URL
|
|
2493
|
+
if (currentUrl && currentUrl.includes('error=')) {
|
|
2494
|
+
if (currentUrl !== lastErrorUrl) {
|
|
2495
|
+
lastErrorUrl = currentUrl;
|
|
2496
|
+
const errorMatch = currentUrl.match(/[?&]error=([^&]+)/);
|
|
2497
|
+
const errorDescMatch = currentUrl.match(/error_description=([^&]+)/);
|
|
2498
|
+
const error = errorMatch && errorMatch[1] ? decodeURIComponent(errorMatch[1]) : 'unknown_error';
|
|
2499
|
+
const errorDesc = errorDescMatch && errorDescMatch[1] ? decodeURIComponent(errorDescMatch[1]) : 'No description';
|
|
2500
|
+
|
|
2501
|
+
// Log error only once
|
|
2502
|
+
if (!oAuthErrorLogged) {
|
|
2503
|
+
console.error('❌ OAuth authentication error:', `${error} - ${errorDesc}`);
|
|
2504
|
+
console.error('💡 The configuration server will remain running. Please try again with correct credentials.');
|
|
2505
|
+
console.error('🔍 Continuing to monitor browser for authorization code...');
|
|
2506
|
+
oAuthErrorLogged = true;
|
|
2507
|
+
}
|
|
2508
|
+
// Continue monitoring silently - don't throw, just keep checking
|
|
2509
|
+
}
|
|
2510
|
+
// If it's the same error URL, continue monitoring silently
|
|
2511
|
+
} else {
|
|
2512
|
+
// Reset error tracking if URL no longer contains error
|
|
2513
|
+
if (lastErrorUrl !== null) {
|
|
2514
|
+
lastErrorUrl = null;
|
|
2515
|
+
oAuthErrorLogged = false; // Reset so we can log new errors
|
|
2516
|
+
}
|
|
2517
|
+
}
|
|
2518
|
+
|
|
2519
|
+
// Wait before checking again
|
|
2520
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
2521
|
+
|
|
2522
|
+
} catch (error: any) {
|
|
2523
|
+
// Only handle non-OAuth errors here (OAuth errors are handled above)
|
|
2524
|
+
if (!(error instanceof Error && error.message.includes('OAuth error:'))) {
|
|
2525
|
+
console.error('❌ Authentication monitoring error:', error);
|
|
2526
|
+
this.stopConfigServer();
|
|
2527
|
+
return; // Exit the loop
|
|
2528
|
+
}
|
|
2529
|
+
// OAuth errors are handled in the main loop above
|
|
2530
|
+
}
|
|
2531
|
+
}
|
|
2532
|
+
}
|
|
2533
|
+
|
|
2560
2534
|
/**
|
|
2561
2535
|
* Stop the configuration HTTP server
|
|
2562
2536
|
*/
|
|
@@ -2740,6 +2714,8 @@ export class AuthenticationHook implements SDKInitHook, BeforeRequestHook {
|
|
|
2740
2714
|
}
|
|
2741
2715
|
} else {
|
|
2742
2716
|
console.error('⏰ Existing token is expired or not found, proceeding with fresh login...');
|
|
2717
|
+
// Clear in-memory token to ensure fresh authentication
|
|
2718
|
+
this.token = null;
|
|
2743
2719
|
}
|
|
2744
2720
|
|
|
2745
2721
|
// Check if we have configuration (from .env or file)
|
package/src/lib/config.ts
CHANGED
|
@@ -82,8 +82,8 @@ export function serverURLFromOptions(options: SDKOptions): URL | null {
|
|
|
82
82
|
export const SDK_METADATA = {
|
|
83
83
|
language: "typescript",
|
|
84
84
|
openapiDocVersion: "1.0.0",
|
|
85
|
-
sdkVersion: "1.0.
|
|
85
|
+
sdkVersion: "1.0.12",
|
|
86
86
|
genVersion: "2.723.8",
|
|
87
87
|
userAgent:
|
|
88
|
-
"speakeasy-sdk/mcp-typescript 1.0.
|
|
88
|
+
"speakeasy-sdk/mcp-typescript 1.0.12 2.723.8 1.0.0 @egain/egain-mcp-server",
|
|
89
89
|
} as const;
|
package/src/mcp-server/server.ts
CHANGED
|
@@ -14,14 +14,55 @@ export const tool$getPortals: ToolDefinition<typeof args> = {
|
|
|
14
14
|
name: "get-portals",
|
|
15
15
|
description: `Get All Portals Accessible To User
|
|
16
16
|
|
|
17
|
+
Get All Portals Accessible to User
|
|
18
|
+
|
|
17
19
|
## Overview
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
The Get All Portals Accessible to User API allows a user to fetch all portals accessible to the user across all departments.
|
|
21
|
+
- If no access tags are specified for a portal, any user can access the portal.
|
|
22
|
+
- If access tags are specified for a portal, users with a user profile that allows access can access the portal. For users with multiple user profiles, the user profile that allows access does not need to be the active user profile.
|
|
23
|
+
- Global users (partition) cannot be assigned user profiles; their access is limited to portals without access restrictions.
|
|
24
|
+
- The only articles returned are associated to an Article type when the parameter “Include in browse on portals” is set to "Yes".
|
|
25
|
+
- When the \`shortUrlTemplate\` query parameter is provided, the API filters accessible portals according to the specified language and template name. A portal short URL specific to the \`shortUrlTemplate\` value is returned in the response when available. If there is no short URL for a language, the portal object returns an empty \`shortURL\` field.
|
|
26
|
+
|
|
27
|
+
## Pagination behavior (CRITICAL for AI assistants)
|
|
28
|
+
|
|
29
|
+
**IMPORTANT**: This endpoint is paginated. When searching for a portal by name or listing portals, you MUST automatically fetch ALL pages before concluding that a portal doesn't exist.
|
|
30
|
+
|
|
31
|
+
### Automatic pagination is REQUIRED when:
|
|
32
|
+
- User asks to find a portal by name (e.g., "business portal", "Master portal")
|
|
33
|
+
- User requests to list or see all portals
|
|
34
|
+
- You need to resolve a natural portal name to its ID
|
|
35
|
+
|
|
36
|
+
### How to detect more pages exist:
|
|
37
|
+
The response includes \`paginationInfo\` with:
|
|
38
|
+
- \`count\`: Total number of items across all pages
|
|
39
|
+
- \`pagenum\`: Current page number
|
|
40
|
+
- \`pagesize\`: Items per page (default: 25)
|
|
41
|
+
|
|
42
|
+
**Check for more pages if ANY of these are true:**
|
|
43
|
+
1. The number of portals returned equals \`pagesize\` (e.g., exactly 25 portals returned)
|
|
44
|
+
2. \`paginationInfo.count > (pagenum * pagesize)\` - there are more items beyond this page
|
|
45
|
+
3. The response includes a \`link\` array with a \`next\` relation
|
|
46
|
+
|
|
47
|
+
### Required pagination workflow:
|
|
48
|
+
1. Start with \`$pagenum=1\` and \`$pagesize=25\` (default)
|
|
49
|
+
2. After receiving the response, check \`paginationInfo\`
|
|
50
|
+
3. **If more pages exist** (using the checks above), automatically call this endpoint again with \`$pagenum=2\`, then \`$pagenum=3\`, etc.
|
|
51
|
+
4. Continue incrementing \`$pagenum\` until:
|
|
52
|
+
- A page returns fewer portals than \`pagesize\` (indicating the last page)
|
|
53
|
+
- A page returns zero portals
|
|
54
|
+
- \`pagenum * pagesize >= paginationInfo.count\` (if count represents total items)
|
|
55
|
+
5. Merge all portals from all pages by unique portal ID
|
|
56
|
+
6. Only then search through the complete merged list or report results to the user
|
|
57
|
+
|
|
58
|
+
### Example scenario:
|
|
59
|
+
If you search for "business portal" and the first page returns 25 portals but none match:
|
|
60
|
+
- DO NOT immediately tell the user the portal doesn't exist
|
|
61
|
+
- Check \`paginationInfo.count\` - if it's > 25, automatically fetch page 2
|
|
62
|
+
- Continue fetching until all pages are retrieved
|
|
63
|
+
- Search the complete merged list before concluding the portal doesn't exist
|
|
64
|
+
|
|
65
|
+
This ensures reliable portal name-to-ID resolution and prevents false "not found" errors.
|
|
25
66
|
`,
|
|
26
67
|
annotations: {
|
|
27
68
|
"destructiveHint": false,
|
|
@@ -27,8 +27,8 @@ export type GetMyPortalsRequest = {
|
|
|
27
27
|
shortUrlTemplate?: string | undefined;
|
|
28
28
|
Dollar_sort?: SortIdNameDepartment | undefined;
|
|
29
29
|
Dollar_order?: Order | undefined;
|
|
30
|
-
Dollar_pagenum?: number | undefined;
|
|
31
30
|
Dollar_pagesize?: number | undefined;
|
|
31
|
+
Dollar_pagenum?: number | undefined;
|
|
32
32
|
};
|
|
33
33
|
|
|
34
34
|
export const GetMyPortalsRequest$zodSchema: z.ZodType<
|