simpleClientCredentials.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env node
  2. /**
  3. * Example demonstrating client_credentials grant for machine-to-machine authentication.
  4. *
  5. * Supports two authentication methods based on environment variables:
  6. *
  7. * 1. client_secret_basic (default):
  8. * MCP_CLIENT_ID - OAuth client ID (required)
  9. * MCP_CLIENT_SECRET - OAuth client secret (required)
  10. *
  11. * 2. private_key_jwt (when MCP_CLIENT_PRIVATE_KEY_PEM is set):
  12. * MCP_CLIENT_ID - OAuth client ID (required)
  13. * MCP_CLIENT_PRIVATE_KEY_PEM - PEM-encoded private key for JWT signing (required)
  14. * MCP_CLIENT_ALGORITHM - Signing algorithm (default: RS256)
  15. *
  16. * Common:
  17. * MCP_SERVER_URL - Server URL (default: http://localhost:3000/mcp)
  18. */
  19. import { Client } from '../../client/index.js';
  20. import { StreamableHTTPClientTransport } from '../../client/streamableHttp.js';
  21. import { ClientCredentialsProvider, PrivateKeyJwtProvider } from '../../client/auth-extensions.js';
  22. const DEFAULT_SERVER_URL = process.env.MCP_SERVER_URL || 'http://localhost:3000/mcp';
  23. function createProvider() {
  24. const clientId = process.env.MCP_CLIENT_ID;
  25. if (!clientId) {
  26. console.error('MCP_CLIENT_ID environment variable is required');
  27. process.exit(1);
  28. }
  29. // If private key is provided, use private_key_jwt authentication
  30. const privateKeyPem = process.env.MCP_CLIENT_PRIVATE_KEY_PEM;
  31. if (privateKeyPem) {
  32. const algorithm = process.env.MCP_CLIENT_ALGORITHM || 'RS256';
  33. console.log('Using private_key_jwt authentication');
  34. return new PrivateKeyJwtProvider({
  35. clientId,
  36. privateKey: privateKeyPem,
  37. algorithm
  38. });
  39. }
  40. // Otherwise, use client_secret_basic authentication
  41. const clientSecret = process.env.MCP_CLIENT_SECRET;
  42. if (!clientSecret) {
  43. console.error('MCP_CLIENT_SECRET or MCP_CLIENT_PRIVATE_KEY_PEM environment variable is required');
  44. process.exit(1);
  45. }
  46. console.log('Using client_secret_basic authentication');
  47. return new ClientCredentialsProvider({
  48. clientId,
  49. clientSecret
  50. });
  51. }
  52. async function main() {
  53. const provider = createProvider();
  54. const client = new Client({ name: 'client-credentials-example', version: '1.0.0' }, { capabilities: {} });
  55. const transport = new StreamableHTTPClientTransport(new URL(DEFAULT_SERVER_URL), {
  56. authProvider: provider
  57. });
  58. await client.connect(transport);
  59. console.log('Connected successfully.');
  60. const tools = await client.listTools();
  61. console.log('Available tools:', tools.tools.map(t => t.name).join(', ') || '(none)');
  62. await transport.close();
  63. }
  64. main().catch(err => {
  65. console.error(err);
  66. process.exit(1);
  67. });
  68. //# sourceMappingURL=simpleClientCredentials.js.map