simpleClientCredentials.js 2.8 KB

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