Coverage for src/mcp_atlassian/__init__.py: 44%

1 import asyncio

2 import logging

3 import os

4 import sys

5

6 import click

7 from dotenv import load_dotenv

8

9 __version__ = "0.1.16"

10

11 logger = logging.getLogger("mcp-atlassian")

12

13

14@click.command()

15@click.option(

16 "-v",

17 "--verbose",

18 count=True,

19 help="Increase verbosity (can be used multiple times)",

20)

21@click.option(

22 "--env-file", type=click.Path(exists=True, dir_okay=False), help="Path to .env file"

23)

24@click.option(

25 "--confluence-url",

26 help="Confluence URL (e.g., https://your-domain.atlassian.net/wiki)",

27)

28@click.option("--confluence-username", help="Confluence username/email")

29@click.option("--confluence-token", help="Confluence API token")

30@click.option(

31 "--jira-url",

32 help="Jira URL (e.g., https://your-domain.atlassian.net or https://jira.your-company.com)",

33)

34@click.option("--jira-username", help="Jira username/email (for Jira Cloud)")

35@click.option("--jira-token", help="Jira API token (for Jira Cloud)")

36@click.option(

37 "--jira-personal-token",

38 help="Jira Personal Access Token (for Jira Server/Data Center)",

39)

40@click.option(

41 "--jira-ssl-verify/--no-jira-ssl-verify",

42 default=True,

43 help="Verify SSL certificates for Jira Server/Data Center (default: verify)",

44)

45 def main(

46 *,

47 verbose: bool = False,

48 env_file: str | None = None,

49 confluence_url: str | None = None,

50 confluence_username: str | None = None,

51 confluence_token: str | None = None,

52 jira_url: str | None = None,

53 jira_username: str | None = None,

54 jira_token: str | None = None,

55 jira_personal_token: str | None = None,

56 jira_ssl_verify: bool = True,

57) -> None:

58 """MCP Atlassian Server - Jira and Confluence functionality for MCP

59

60 Supports both Atlassian Cloud and Jira Server/Data Center deployments.

61

62 Args:

63 verbose: Enable verbose logging output

64 env_file: Path to .env file for configuration

65 confluence_url: Confluence URL

66 confluence_username: Confluence username

67 confluence_token: Confluence API token

68 jira_url: Jira URL

69 jira_username: Jira username/email (for Jira Cloud)

70 jira_token: Jira API token (for Jira Cloud)

71 jira_personal_token: Jira personal access token (for Jira Server/Data Center)

72 jira_ssl_verify: Whether to verify SSL certificates for Jira connections

73 """

74 # Set up logging based on verbosity

75 _setup_logging(verbose=verbose)

76

77 # Load environment variables

78 _load_environment_variables(env_file=env_file)

79

80 # Set environment variables from command line arguments

81 _set_environment_from_args(

82 confluence_url=confluence_url,

83 confluence_username=confluence_username,

84 confluence_token=confluence_token,

85 jira_url=jira_url,

86 jira_username=jira_username,

87 jira_token=jira_token,

88 jira_personal_token=jira_personal_token,

89 jira_ssl_verify=jira_ssl_verify,

90 )

91

92 # Import the server module after environment setup

93 from . import server

94

95 # Run the server

96 asyncio.run(server.main())

97

98

99 def _setup_logging(*, verbose: int) -> None:

100 """

101 Configure logging based on verbosity level.

102

103 Args:

104 verbose: Verbosity level (0=INFO, 1=INFO, 2+=DEBUG)

105 """

106 logging_level = logging.INFO

107 if verbose == 1:

108 logging_level = logging.INFO

109 elif verbose >= 2:

110 logging_level = logging.DEBUG

111

112 logging.basicConfig(level=logging_level, stream=sys.stderr)

113

114

115 def _load_environment_variables(*, env_file: str | None = None) -> None:

116 """

117 Load environment variables from file.

118

119 Args:

120 env_file: Optional path to .env file

121 """

122 if env_file:

123 logger.debug(f"Loading environment from file: {env_file}")

124 load_dotenv(env_file)

125 else:

126 logger.debug("Attempting to load environment from default .env file")

127 load_dotenv()

128

129

130 def _set_environment_from_args(

131 *,

132 confluence_url: str | None = None,

133 confluence_username: str | None = None,

134 confluence_token: str | None = None,

135 jira_url: str | None = None,

136 jira_username: str | None = None,

137 jira_token: str | None = None,

138 jira_personal_token: str | None = None,

139 jira_ssl_verify: bool = True,

140) -> None:

141 """

142 Set environment variables from command line arguments.

143

144 Args:

145 confluence_url: Confluence URL

146 confluence_username: Confluence username

147 confluence_token: Confluence API token

148 jira_url: Jira URL

149 jira_username: Jira username

150 jira_token: Jira API token

151 jira_personal_token: Jira personal access token

152 jira_ssl_verify: Whether to verify SSL certificates for Jira connections

153 """

154 # Set environment variables from command line arguments if provided

155 if confluence_url:

156 os.environ["CONFLUENCE_URL"] = confluence_url

157 if confluence_username:

158 os.environ["CONFLUENCE_USERNAME"] = confluence_username

159 if confluence_token:

160 os.environ["CONFLUENCE_API_TOKEN"] = confluence_token

161 if jira_url:

162 os.environ["JIRA_URL"] = jira_url

163 if jira_username:

164 os.environ["JIRA_USERNAME"] = jira_username

165 if jira_token:

166 os.environ["JIRA_API_TOKEN"] = jira_token

167 if jira_personal_token:

168 os.environ["JIRA_PERSONAL_TOKEN"] = jira_personal_token

169

170 # Set SSL verification for Jira Server/Data Center

171 os.environ["JIRA_SSL_VERIFY"] = str(jira_ssl_verify).lower()

172

173

174 __all__ = ["main", "server", "__version__"]

175

176 if __name__ == "__main__":

177 main()