Abid Ali Awan
commited on
Commit
Β·
53e0bdc
1
Parent(s):
ed72e55
Update README.md to enhance project description, setup instructions, and connection details for the Code Analysis MCP server. Adjusted title, emoji, and SDK version, and clarified usage with Cursor AI.
Browse files- .gitignore +194 -0
- LICENSE +201 -0
- README.md +62 -7
- requirements.txt +6 -0
- src/app.py +34 -0
- src/code_analyzer/__init__.py +1 -0
- src/code_analyzer/analysis.py +123 -0
- src/code_analyzer/scoring.py +246 -0
- tests/test_app.py +18 -0
.gitignore
ADDED
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# UV
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
#uv.lock
|
102 |
+
|
103 |
+
# poetry
|
104 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
105 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
106 |
+
# commonly ignored for libraries.
|
107 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
108 |
+
#poetry.lock
|
109 |
+
|
110 |
+
# pdm
|
111 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
112 |
+
#pdm.lock
|
113 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
114 |
+
# in version control.
|
115 |
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
116 |
+
.pdm.toml
|
117 |
+
.pdm-python
|
118 |
+
.pdm-build/
|
119 |
+
|
120 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
121 |
+
__pypackages__/
|
122 |
+
|
123 |
+
# Celery stuff
|
124 |
+
celerybeat-schedule
|
125 |
+
celerybeat.pid
|
126 |
+
|
127 |
+
# SageMath parsed files
|
128 |
+
*.sage.py
|
129 |
+
|
130 |
+
# Environments
|
131 |
+
.env
|
132 |
+
.venv
|
133 |
+
env/
|
134 |
+
venv/
|
135 |
+
ENV/
|
136 |
+
env.bak/
|
137 |
+
venv.bak/
|
138 |
+
|
139 |
+
# Spyder project settings
|
140 |
+
.spyderproject
|
141 |
+
.spyproject
|
142 |
+
|
143 |
+
# Rope project settings
|
144 |
+
.ropeproject
|
145 |
+
|
146 |
+
# mkdocs documentation
|
147 |
+
/site
|
148 |
+
|
149 |
+
# mypy
|
150 |
+
.mypy_cache/
|
151 |
+
.dmypy.json
|
152 |
+
dmypy.json
|
153 |
+
|
154 |
+
# Pyre type checker
|
155 |
+
.pyre/
|
156 |
+
|
157 |
+
# pytype static type analyzer
|
158 |
+
.pytype/
|
159 |
+
|
160 |
+
# Cython debug symbols
|
161 |
+
cython_debug/
|
162 |
+
|
163 |
+
# PyCharm
|
164 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
165 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
166 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
167 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
168 |
+
#.idea/
|
169 |
+
|
170 |
+
# Abstra
|
171 |
+
# Abstra is an AI-powered process automation framework.
|
172 |
+
# Ignore directories containing user credentials, local state, and settings.
|
173 |
+
# Learn more at https://abstra.io/docs
|
174 |
+
.abstra/
|
175 |
+
|
176 |
+
# Visual Studio Code
|
177 |
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
178 |
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
179 |
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
180 |
+
# you could uncomment the following to ignore the enitre vscode folder
|
181 |
+
# .vscode/
|
182 |
+
|
183 |
+
# Ruff stuff:
|
184 |
+
.ruff_cache/
|
185 |
+
|
186 |
+
# PyPI configuration file
|
187 |
+
.pypirc
|
188 |
+
|
189 |
+
# Cursor
|
190 |
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
191 |
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
192 |
+
# refer to https://docs.cursor.com/context/ignore-files
|
193 |
+
.cursorignore
|
194 |
+
.cursorindexingignore
|
LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Apache License
|
2 |
+
Version 2.0, January 2004
|
3 |
+
http://www.apache.org/licenses/
|
4 |
+
|
5 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6 |
+
|
7 |
+
1. Definitions.
|
8 |
+
|
9 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
11 |
+
|
12 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13 |
+
the copyright owner that is granting the License.
|
14 |
+
|
15 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
16 |
+
other entities that control, are controlled by, or are under common
|
17 |
+
control with that entity. For the purposes of this definition,
|
18 |
+
"control" means (i) the power, direct or indirect, to cause the
|
19 |
+
direction or management of such entity, whether by contract or
|
20 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22 |
+
|
23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24 |
+
exercising permissions granted by this License.
|
25 |
+
|
26 |
+
"Source" form shall mean the preferred form for making modifications,
|
27 |
+
including but not limited to software source code, documentation
|
28 |
+
source, and configuration files.
|
29 |
+
|
30 |
+
"Object" form shall mean any form resulting from mechanical
|
31 |
+
transformation or translation of a Source form, including but
|
32 |
+
not limited to compiled object code, generated documentation,
|
33 |
+
and conversions to other media types.
|
34 |
+
|
35 |
+
"Work" shall mean the work of authorship, whether in Source or
|
36 |
+
Object form, made available under the License, as indicated by a
|
37 |
+
copyright notice that is included in or attached to the work
|
38 |
+
(an example is provided in the Appendix below).
|
39 |
+
|
40 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41 |
+
form, that is based on (or derived from) the Work and for which the
|
42 |
+
editorial revisions, annotations, elaborations, or other modifications
|
43 |
+
represent, as a whole, an original work of authorship. For the purposes
|
44 |
+
of this License, Derivative Works shall not include works that remain
|
45 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46 |
+
the Work and Derivative Works thereof.
|
47 |
+
|
48 |
+
"Contribution" shall mean any work of authorship, including
|
49 |
+
the original version of the Work and any modifications or additions
|
50 |
+
to that Work or Derivative Works thereof, that is intentionally
|
51 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
54 |
+
means any form of electronic, verbal, or written communication sent
|
55 |
+
to the Licensor or its representatives, including but not limited to
|
56 |
+
communication on electronic mailing lists, source code control systems,
|
57 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
58 |
+
Licensor for the purpose of discussing and improving the Work, but
|
59 |
+
excluding communication that is conspicuously marked or otherwise
|
60 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
61 |
+
|
62 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63 |
+
on behalf of whom a Contribution has been received by Licensor and
|
64 |
+
subsequently incorporated within the Work.
|
65 |
+
|
66 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67 |
+
this License, each Contributor hereby grants to You a perpetual,
|
68 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69 |
+
copyright license to reproduce, prepare Derivative Works of,
|
70 |
+
publicly display, publicly perform, sublicense, and distribute the
|
71 |
+
Work and such Derivative Works in Source or Object form.
|
72 |
+
|
73 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74 |
+
this License, each Contributor hereby grants to You a perpetual,
|
75 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76 |
+
(except as stated in this section) patent license to make, have made,
|
77 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78 |
+
where such license applies only to those patent claims licensable
|
79 |
+
by such Contributor that are necessarily infringed by their
|
80 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
81 |
+
with the Work to which such Contribution(s) was submitted. If You
|
82 |
+
institute patent litigation against any entity (including a
|
83 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84 |
+
or a Contribution incorporated within the Work constitutes direct
|
85 |
+
or contributory patent infringement, then any patent licenses
|
86 |
+
granted to You under this License for that Work shall terminate
|
87 |
+
as of the date such litigation is filed.
|
88 |
+
|
89 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
90 |
+
Work or Derivative Works thereof in any medium, with or without
|
91 |
+
modifications, and in Source or Object form, provided that You
|
92 |
+
meet the following conditions:
|
93 |
+
|
94 |
+
(a) You must give any other recipients of the Work or
|
95 |
+
Derivative Works a copy of this License; and
|
96 |
+
|
97 |
+
(b) You must cause any modified files to carry prominent notices
|
98 |
+
stating that You changed the files; and
|
99 |
+
|
100 |
+
(c) You must retain, in the Source form of any Derivative Works
|
101 |
+
that You distribute, all copyright, patent, trademark, and
|
102 |
+
attribution notices from the Source form of the Work,
|
103 |
+
excluding those notices that do not pertain to any part of
|
104 |
+
the Derivative Works; and
|
105 |
+
|
106 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107 |
+
distribution, then any Derivative Works that You distribute must
|
108 |
+
include a readable copy of the attribution notices contained
|
109 |
+
within such NOTICE file, excluding those notices that do not
|
110 |
+
pertain to any part of the Derivative Works, in at least one
|
111 |
+
of the following places: within a NOTICE text file distributed
|
112 |
+
as part of the Derivative Works; within the Source form or
|
113 |
+
documentation, if provided along with the Derivative Works; or,
|
114 |
+
within a display generated by the Derivative Works, if and
|
115 |
+
wherever such third-party notices normally appear. The contents
|
116 |
+
of the NOTICE file are for informational purposes only and
|
117 |
+
do not modify the License. You may add Your own attribution
|
118 |
+
notices within Derivative Works that You distribute, alongside
|
119 |
+
or as an addendum to the NOTICE text from the Work, provided
|
120 |
+
that such additional attribution notices cannot be construed
|
121 |
+
as modifying the License.
|
122 |
+
|
123 |
+
You may add Your own copyright statement to Your modifications and
|
124 |
+
may provide additional or different license terms and conditions
|
125 |
+
for use, reproduction, or distribution of Your modifications, or
|
126 |
+
for any such Derivative Works as a whole, provided Your use,
|
127 |
+
reproduction, and distribution of the Work otherwise complies with
|
128 |
+
the conditions stated in this License.
|
129 |
+
|
130 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131 |
+
any Contribution intentionally submitted for inclusion in the Work
|
132 |
+
by You to the Licensor shall be under the terms and conditions of
|
133 |
+
this License, without any additional terms or conditions.
|
134 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135 |
+
the terms of any separate license agreement you may have executed
|
136 |
+
with Licensor regarding such Contributions.
|
137 |
+
|
138 |
+
6. Trademarks. This License does not grant permission to use the trade
|
139 |
+
names, trademarks, service marks, or product names of the Licensor,
|
140 |
+
except as required for reasonable and customary use in describing the
|
141 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
142 |
+
|
143 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144 |
+
agreed to in writing, Licensor provides the Work (and each
|
145 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147 |
+
implied, including, without limitation, any warranties or conditions
|
148 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150 |
+
appropriateness of using or redistributing the Work and assume any
|
151 |
+
risks associated with Your exercise of permissions under this License.
|
152 |
+
|
153 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
154 |
+
whether in tort (including negligence), contract, or otherwise,
|
155 |
+
unless required by applicable law (such as deliberate and grossly
|
156 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157 |
+
liable to You for damages, including any direct, indirect, special,
|
158 |
+
incidental, or consequential damages of any character arising as a
|
159 |
+
result of this License or out of the use or inability to use the
|
160 |
+
Work (including but not limited to damages for loss of goodwill,
|
161 |
+
work stoppage, computer failure or malfunction, or any and all
|
162 |
+
other commercial damages or losses), even if such Contributor
|
163 |
+
has been advised of the possibility of such damages.
|
164 |
+
|
165 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
167 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168 |
+
or other liability obligations and/or rights consistent with this
|
169 |
+
License. However, in accepting such obligations, You may act only
|
170 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171 |
+
of any other Contributor, and only if You agree to indemnify,
|
172 |
+
defend, and hold each Contributor harmless for any liability
|
173 |
+
incurred by, or claims asserted against, such Contributor by reason
|
174 |
+
of your accepting any such warranty or additional liability.
|
175 |
+
|
176 |
+
END OF TERMS AND CONDITIONS
|
177 |
+
|
178 |
+
APPENDIX: How to apply the Apache License to your work.
|
179 |
+
|
180 |
+
To apply the Apache License to your work, attach the following
|
181 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182 |
+
replaced with your own identifying information. (Don't include
|
183 |
+
the brackets!) The text should be enclosed in the appropriate
|
184 |
+
comment syntax for the file format. We also recommend that a
|
185 |
+
file or class name and description of purpose be included on the
|
186 |
+
same "printed page" as the copyright notice for easier
|
187 |
+
identification within third-party archives.
|
188 |
+
|
189 |
+
Copyright [yyyy] [name of copyright owner]
|
190 |
+
|
191 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192 |
+
you may not use this file except in compliance with the License.
|
193 |
+
You may obtain a copy of the License at
|
194 |
+
|
195 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
196 |
+
|
197 |
+
Unless required by applicable law or agreed to in writing, software
|
198 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200 |
+
See the License for the specific language governing permissions and
|
201 |
+
limitations under the License.
|
README.md
CHANGED
@@ -1,14 +1,69 @@
|
|
1 |
---
|
2 |
-
title: Code Analysis
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 5.
|
8 |
-
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
short_description: Generate quality metrics and a detailed report for your code
|
12 |
---
|
13 |
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Code Analysis MCP
|
3 |
+
emoji: π§βπ»
|
4 |
+
colorFrom: black
|
5 |
+
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 5.32.1
|
8 |
+
app_file: src/app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
short_description: Generate quality metrics and a detailed report for your code
|
12 |
---
|
13 |
|
14 |
+
|
15 |
+
# Code Analysis MCP Server
|
16 |
+
|
17 |
+
This project is a Gradio-based MCP server that provides two code analysis functionalities:
|
18 |
+
|
19 |
+
- **Code Quality Score**: Provides an averaged score across vulnerability, style, and quality for the provided code using top three AI providers.
|
20 |
+
- **Code Analysis Report**: Generates a detailed report about the provided code, including basic information and suggesting 5-10 potential fixes to improve the code.
|
21 |
+
|
22 |
+
## Setup and Running
|
23 |
+
|
24 |
+
1. Clone the repository.
|
25 |
+
2. Navigate to the project directory.
|
26 |
+
3. Install the required dependencies:
|
27 |
+
|
28 |
+
```bash
|
29 |
+
pip install -r requirements.txt
|
30 |
+
```
|
31 |
+
|
32 |
+
4. Run the application:
|
33 |
+
|
34 |
+
```bash
|
35 |
+
python src/app.py
|
36 |
+
```
|
37 |
+
|
38 |
+
5. The Gradio interface will be available at `http://127.0.0.1:7860/` and MCP server will be avaible at `http://127.0.0.1:7860/gradio_api/mcp/sse`.
|
39 |
+
|
40 |
+
## Connecting to Cursor AI
|
41 |
+
|
42 |
+
7. To test the MCP server with Cursor AI, open Cursor Settings, navigate to the "MCP" tab, and click the "+ Add new global MCP server" button.
|
43 |
+
|
44 |
+
8. Add the following JSON configuration to the MCP settings file:
|
45 |
+
```json
|
46 |
+
{
|
47 |
+
"mcpServers": {
|
48 |
+
"gradio": {
|
49 |
+
"url": "http://127.0.0.1:7860/gradio_api/mcp/sse"
|
50 |
+
}
|
51 |
+
}
|
52 |
+
}
|
53 |
+
```
|
54 |
+
|
55 |
+
9. Save the file. You will now see an active MCP server named `gradio` with the tools `code_analysis_report` and `code_analysis_score`.
|
56 |
+
|
57 |
+
|
58 |
+
To test this MCP server, you can create a new chat in agent mode of the Cursor using (CTRL +T) and ask for a code analysis report (e.g., "analyze this Python code: print('hello')"). Cursor will ask for permission to run the MCP tool. Approve it.
|
59 |
+
|
60 |
+
### Sample Prompts
|
61 |
+
|
62 |
+
Here are a few ways you can ask Cursor AI to use these tools:
|
63 |
+
|
64 |
+
* "Can you give me a code quality score for this Python snippet?"
|
65 |
+
* "Generate a code analysis report for the following JavaScript code."
|
66 |
+
* "Analyze this code and tell me how to fix the top issues."
|
67 |
+
* "What is the quality score of this code?"
|
68 |
+
|
69 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
python-dotenv>=1.1.0
|
2 |
+
gradio==5.32.1
|
3 |
+
gradio[mcp]==5.32.1
|
4 |
+
mistralai==1.8.1
|
5 |
+
openai==1.84.0
|
6 |
+
anthropic==0.52.2
|
src/app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
|
4 |
+
from code_analyzer.analysis import code_analysis_report
|
5 |
+
from code_analyzer.scoring import code_analysis_score
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
|
10 |
+
# Create Gradio interfaces for code analysis
|
11 |
+
analysis_report_demo = gr.Interface(
|
12 |
+
fn=code_analysis_report,
|
13 |
+
inputs=gr.Textbox(label="Enter Code Here", lines=20),
|
14 |
+
outputs=gr.Textbox(label="Analysis Report", lines=20),
|
15 |
+
description="Generate a basic code analysis report.",
|
16 |
+
)
|
17 |
+
|
18 |
+
code_score_demo = gr.Interface(
|
19 |
+
fn=code_analysis_score,
|
20 |
+
inputs=gr.Textbox(label="Enter Code Here", lines=20),
|
21 |
+
outputs=gr.JSON(label="Code Score"),
|
22 |
+
description="Generate a basic code score.",
|
23 |
+
)
|
24 |
+
|
25 |
+
# Create tabbed interface
|
26 |
+
demo = gr.TabbedInterface(
|
27 |
+
[analysis_report_demo, code_score_demo],
|
28 |
+
["Code Analysis Report", "Code Score"],
|
29 |
+
title="Code Analysis Server",
|
30 |
+
)
|
31 |
+
|
32 |
+
if __name__ == "__main__":
|
33 |
+
# Launch the Gradio interface
|
34 |
+
demo.launch(share=False, mcp_server=True, debug=True)
|
src/code_analyzer/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
|
src/code_analyzer/analysis.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from anthropic import Anthropic
|
4 |
+
|
5 |
+
|
6 |
+
def code_analysis_report(code: str) -> str:
|
7 |
+
"""
|
8 |
+
Generate a code analysis report using the Anthropic API.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
code (str): The code string to analyze.
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
str: A detailed analysis report including top fixes.
|
15 |
+
"""
|
16 |
+
if not code:
|
17 |
+
return "Please provide code to analyze."
|
18 |
+
|
19 |
+
try:
|
20 |
+
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
|
21 |
+
|
22 |
+
analysis_prompt = (
|
23 |
+
"""Analyze the following code for potential issues, including vulnerabilities, style problems, and quality concerns. """.strip()
|
24 |
+
+ f""" Provide a detailed analysis and list the top 5-10 most critical fixes needed, explaining each one.
|
25 |
+
```python
|
26 |
+
{code}
|
27 |
+
```""".strip()
|
28 |
+
)
|
29 |
+
|
30 |
+
# Define a tool to structure the output
|
31 |
+
tools = [
|
32 |
+
{
|
33 |
+
"name": "analysis_report",
|
34 |
+
"description": "Return the detailed analysis and a list of top fixes.",
|
35 |
+
"input_schema": {
|
36 |
+
"type": "object",
|
37 |
+
"properties": {
|
38 |
+
"detailed_analysis": {
|
39 |
+
"type": "string",
|
40 |
+
"description": "Detailed analysis of the code.",
|
41 |
+
},
|
42 |
+
"top_fixes": {
|
43 |
+
"type": "array",
|
44 |
+
"description": "List of top fixes needed.",
|
45 |
+
"items": {
|
46 |
+
"type": "object",
|
47 |
+
"properties": {
|
48 |
+
"fix_description": {
|
49 |
+
"type": "string",
|
50 |
+
"description": "Description of the fix.",
|
51 |
+
},
|
52 |
+
"severity": {
|
53 |
+
"type": "string",
|
54 |
+
"description": "Severity of the issue (e.g., Critical, High, Medium, Low).",
|
55 |
+
},
|
56 |
+
},
|
57 |
+
"required": ["fix_description", "severity"],
|
58 |
+
},
|
59 |
+
},
|
60 |
+
},
|
61 |
+
"required": ["detailed_analysis", "top_fixes"],
|
62 |
+
"additionalProperties": False,
|
63 |
+
},
|
64 |
+
}
|
65 |
+
]
|
66 |
+
|
67 |
+
resp = client.messages.create(
|
68 |
+
model="claude-sonnet-4-20250514", # Using the model from scoring.py
|
69 |
+
messages=[{"role": "user", "content": analysis_prompt.format(code=code)}],
|
70 |
+
system="You are a secure-coding assistant that provides detailed analysis and actionable fixes.",
|
71 |
+
tools=tools,
|
72 |
+
tool_choice={"type": "tool", "name": "analysis_report"},
|
73 |
+
max_tokens=1500, # Increased tokens for detailed output
|
74 |
+
temperature=0,
|
75 |
+
)
|
76 |
+
|
77 |
+
# Check if the response contains a tool_use block
|
78 |
+
tool_call = None
|
79 |
+
for content_block in resp.content:
|
80 |
+
if content_block.type == "tool_use":
|
81 |
+
tool_call = content_block
|
82 |
+
break
|
83 |
+
|
84 |
+
if tool_call is None:
|
85 |
+
# If no tool_use block is found, return an error with the raw response content
|
86 |
+
return f"Error during code analysis: Expected tool_use response, but received: {resp.content}"
|
87 |
+
|
88 |
+
analysis_result = tool_call.input
|
89 |
+
|
90 |
+
report = "Code Analysis Report:\n\n"
|
91 |
+
report += "Detailed Analysis:\n"
|
92 |
+
report += analysis_result.get("detailed_analysis", "N/A") + "\n\n"
|
93 |
+
|
94 |
+
report += "Top Fixes:\n"
|
95 |
+
fixes = analysis_result.get("top_fixes", [])
|
96 |
+
if fixes:
|
97 |
+
for i, fix in enumerate(fixes):
|
98 |
+
report += f"{i + 1}. [Severity: {fix.get('severity', 'N/A')}] {fix.get('fix_description', 'N/A')}\n"
|
99 |
+
else:
|
100 |
+
report += "No specific fixes identified or issues found."
|
101 |
+
|
102 |
+
return report
|
103 |
+
|
104 |
+
except Exception as exc:
|
105 |
+
return f"Error during code analysis: {exc}"
|
106 |
+
|
107 |
+
|
108 |
+
# ------------------------------------------------------------------ #
|
109 |
+
# Demo / quick test
|
110 |
+
# ------------------------------------------------------------------ #
|
111 |
+
|
112 |
+
if __name__ == "__main__":
|
113 |
+
sample_code = """
|
114 |
+
def calculate_discount(price, discount_rate):
|
115 |
+
# Applies a discount to a price
|
116 |
+
if discount_rate > 1:
|
117 |
+
return price # No discount if rate is invalid
|
118 |
+
return price * (1 - discount_rate)
|
119 |
+
"""
|
120 |
+
|
121 |
+
print("Analyzing sample code...")
|
122 |
+
analysis_report = code_analysis_report(sample_code)
|
123 |
+
print("\n" + analysis_report)
|
src/code_analyzer/scoring.py
ADDED
@@ -0,0 +1,246 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
from typing import Any, Dict
|
4 |
+
|
5 |
+
from anthropic import Anthropic
|
6 |
+
from mistralai import Mistral
|
7 |
+
from openai import OpenAI
|
8 |
+
from pydantic import BaseModel, ValidationError
|
9 |
+
|
10 |
+
# ------------------------------------------------------------------ #
|
11 |
+
# Configuration
|
12 |
+
# ------------------------------------------------------------------ #
|
13 |
+
|
14 |
+
DEFAULT_SCORES: Dict[str, Any] = {
|
15 |
+
"vulnerability_score": 0,
|
16 |
+
"style_score": 0,
|
17 |
+
"quality_score": 0,
|
18 |
+
}
|
19 |
+
|
20 |
+
ANALYSIS_PROMPT_TEMPLATE = (
|
21 |
+
"Analyze the following code for vulnerabilities, style, and quality "
|
22 |
+
"and return **only** a JSON object with keys "
|
23 |
+
"'vulnerability_score', 'style_score', and 'quality_score' "
|
24 |
+
"(each 0β100):\n```python\n{code}\n```"
|
25 |
+
)
|
26 |
+
|
27 |
+
SYSTEM_MESSAGES = {
|
28 |
+
"anthropic": "You are a secure-coding assistant. Assess code quality, style and vulnerabilities.",
|
29 |
+
"mistral": "You are a secure-coding assistant. Assess code quality, style and vulnerabilities.",
|
30 |
+
"openai": "You are a secure-coding assistant. Assess code quality, style and vulnerabilities.",
|
31 |
+
}
|
32 |
+
|
33 |
+
MODELS = {
|
34 |
+
"anthropic": "claude-sonnet-4-20250514",
|
35 |
+
"mistral": "mistral-medium-2505",
|
36 |
+
"openai": "gpt-4.1-2025-04-14",
|
37 |
+
}
|
38 |
+
|
39 |
+
REQUIRED_KEYS = ("vulnerability_score", "style_score", "quality_score")
|
40 |
+
|
41 |
+
# ------------------------------------------------------------------ #
|
42 |
+
# Helpers
|
43 |
+
# ------------------------------------------------------------------ #
|
44 |
+
|
45 |
+
|
46 |
+
class CodeAnalysisResult(BaseModel):
|
47 |
+
vulnerability_score: int
|
48 |
+
style_score: int
|
49 |
+
quality_score: int
|
50 |
+
|
51 |
+
|
52 |
+
def _safe_json_loads(raw: str) -> Dict[str, Any]:
|
53 |
+
"""
|
54 |
+
Best-effort JSON parsing β fall back to DEFAULT_SCORES on failure.
|
55 |
+
"""
|
56 |
+
try:
|
57 |
+
return json.loads(raw)
|
58 |
+
except json.JSONDecodeError:
|
59 |
+
return DEFAULT_SCORES.copy()
|
60 |
+
|
61 |
+
|
62 |
+
def _ensure_all_keys(d: dict, default: int = 0) -> dict:
|
63 |
+
"""
|
64 |
+
Return a dict that has every REQUIRED_KEYS entry.
|
65 |
+
Missing keys are added with `default`.
|
66 |
+
Non-required keys are discarded.
|
67 |
+
"""
|
68 |
+
return {key: int(d.get(key, default)) for key in REQUIRED_KEYS}
|
69 |
+
|
70 |
+
|
71 |
+
# ------------------------------------------------------------------ #
|
72 |
+
# Provider wrappers
|
73 |
+
# ------------------------------------------------------------------ #
|
74 |
+
|
75 |
+
|
76 |
+
def analyze_code_anthropic(code: str) -> dict:
|
77 |
+
if not code:
|
78 |
+
return _ensure_all_keys({})
|
79 |
+
|
80 |
+
try:
|
81 |
+
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
|
82 |
+
prompt = ANALYSIS_PROMPT_TEMPLATE.format(code=code)
|
83 |
+
|
84 |
+
tools = [
|
85 |
+
{
|
86 |
+
"name": "code_scores",
|
87 |
+
"description": "Return ONLY the three integer scores (0-100).",
|
88 |
+
"input_schema": {
|
89 |
+
"type": "object",
|
90 |
+
"properties": {
|
91 |
+
"vulnerability_score": {
|
92 |
+
"type": "integer",
|
93 |
+
"minimum": 0,
|
94 |
+
"maximum": 100,
|
95 |
+
},
|
96 |
+
"style_score": {
|
97 |
+
"type": "integer",
|
98 |
+
"minimum": 0,
|
99 |
+
"maximum": 100,
|
100 |
+
},
|
101 |
+
"quality_score": {
|
102 |
+
"type": "integer",
|
103 |
+
"minimum": 0,
|
104 |
+
"maximum": 100,
|
105 |
+
},
|
106 |
+
},
|
107 |
+
"required": list(REQUIRED_KEYS),
|
108 |
+
"additionalProperties": False,
|
109 |
+
},
|
110 |
+
}
|
111 |
+
]
|
112 |
+
|
113 |
+
resp = client.messages.create(
|
114 |
+
model=MODELS["anthropic"],
|
115 |
+
messages=[{"role": "user", "content": prompt}],
|
116 |
+
system=SYSTEM_MESSAGES["anthropic"],
|
117 |
+
tools=tools,
|
118 |
+
tool_choice={"type": "tool", "name": "code_scores"},
|
119 |
+
max_tokens=130,
|
120 |
+
temperature=0,
|
121 |
+
)
|
122 |
+
|
123 |
+
tool_call = next(c for c in resp.content if c.type == "tool_use")
|
124 |
+
return _ensure_all_keys(tool_call.input)
|
125 |
+
|
126 |
+
except Exception as exc:
|
127 |
+
out = _ensure_all_keys({})
|
128 |
+
out["error"] = f"Anthropic API error: {exc}"
|
129 |
+
return out
|
130 |
+
|
131 |
+
|
132 |
+
def analyze_code_mistral(code: str) -> Dict[str, Any]:
|
133 |
+
if not code:
|
134 |
+
return DEFAULT_SCORES.copy()
|
135 |
+
|
136 |
+
try:
|
137 |
+
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
|
138 |
+
prompt = ANALYSIS_PROMPT_TEMPLATE.format(code=code)
|
139 |
+
|
140 |
+
resp = client.chat.complete(
|
141 |
+
model=MODELS["mistral"],
|
142 |
+
messages=[
|
143 |
+
{"role": "system", "content": SYSTEM_MESSAGES["mistral"]},
|
144 |
+
{"role": "user", "content": prompt},
|
145 |
+
],
|
146 |
+
response_format={"type": "json_object"},
|
147 |
+
)
|
148 |
+
|
149 |
+
return _safe_json_loads(resp.choices[0].message.content)
|
150 |
+
|
151 |
+
except Exception as exc:
|
152 |
+
result = DEFAULT_SCORES.copy()
|
153 |
+
result["error"] = f"Mistral API error: {exc}"
|
154 |
+
return result
|
155 |
+
|
156 |
+
|
157 |
+
def analyze_code_openai(code: str) -> Dict[str, Any]:
|
158 |
+
if not code:
|
159 |
+
return DEFAULT_SCORES.copy()
|
160 |
+
|
161 |
+
try:
|
162 |
+
client = OpenAI() # uses OPENAI_API_KEY from env
|
163 |
+
prompt = ANALYSIS_PROMPT_TEMPLATE.format(code=code)
|
164 |
+
|
165 |
+
resp = client.chat.completions.create(
|
166 |
+
model=MODELS["openai"],
|
167 |
+
messages=[
|
168 |
+
{"role": "system", "content": SYSTEM_MESSAGES["openai"]},
|
169 |
+
{"role": "user", "content": prompt},
|
170 |
+
],
|
171 |
+
response_format={"type": "json_object"},
|
172 |
+
)
|
173 |
+
|
174 |
+
# Validate via Pydantic (optional but nice)
|
175 |
+
parsed = _safe_json_loads(resp.choices[0].message.content)
|
176 |
+
try:
|
177 |
+
validated = CodeAnalysisResult(**parsed)
|
178 |
+
return validated.model_dump()
|
179 |
+
except ValidationError:
|
180 |
+
# If model returns extra fields or wrong types, fall back to raw
|
181 |
+
return parsed
|
182 |
+
|
183 |
+
except Exception as exc:
|
184 |
+
result = DEFAULT_SCORES.copy()
|
185 |
+
result["error"] = f"OpenAI API error: {exc}"
|
186 |
+
return result
|
187 |
+
|
188 |
+
|
189 |
+
# ------------------------------------------------------------------ #
|
190 |
+
# Aggregator
|
191 |
+
# ------------------------------------------------------------------ #
|
192 |
+
|
193 |
+
|
194 |
+
def code_analysis_score(code: str) -> Dict[str, Any]:
|
195 |
+
"""
|
196 |
+
Analyzes the provided code string using multiple AI providers and returns an
|
197 |
+
averaged score across vulnerability, style, and quality.
|
198 |
+
|
199 |
+
Args:
|
200 |
+
code: The code string to analyze.
|
201 |
+
|
202 |
+
Returns:
|
203 |
+
A dictionary containing the averaged vulnerability, style, and quality scores,
|
204 |
+
or an error message if all providers fail.
|
205 |
+
"""
|
206 |
+
if not code:
|
207 |
+
return DEFAULT_SCORES.copy()
|
208 |
+
|
209 |
+
scores_list = [
|
210 |
+
analyze_code_anthropic(code),
|
211 |
+
analyze_code_mistral(code),
|
212 |
+
analyze_code_openai(code),
|
213 |
+
]
|
214 |
+
valid = [s for s in scores_list if "error" not in s]
|
215 |
+
|
216 |
+
if not valid:
|
217 |
+
result = DEFAULT_SCORES.copy()
|
218 |
+
result["error"] = "All API providers failed"
|
219 |
+
return result
|
220 |
+
|
221 |
+
# Average
|
222 |
+
averaged = {
|
223 |
+
"vulnerability_score": sum(s["vulnerability_score"] for s in valid)
|
224 |
+
// len(valid),
|
225 |
+
"style_score": sum(s["style_score"] for s in valid) // len(valid),
|
226 |
+
"quality_score": sum(s["quality_score"] for s in valid) // len(valid),
|
227 |
+
}
|
228 |
+
return averaged
|
229 |
+
|
230 |
+
|
231 |
+
# ------------------------------------------------------------------ #
|
232 |
+
# Demo / quick test
|
233 |
+
# ------------------------------------------------------------------ #
|
234 |
+
|
235 |
+
if __name__ == "__main__":
|
236 |
+
sample = """
|
237 |
+
def example_function(x):
|
238 |
+
if x is None:
|
239 |
+
return "Error"
|
240 |
+
return x * 2
|
241 |
+
"""
|
242 |
+
|
243 |
+
print("Anthropic β", analyze_code_anthropic(sample))
|
244 |
+
print("Mistral β", analyze_code_mistral(sample))
|
245 |
+
print("OpenAI β", analyze_code_openai(sample))
|
246 |
+
print("AVERAGED β", code_analysis_score(sample))
|
tests/test_app.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from gradio_client import Client
|
2 |
+
|
3 |
+
client = Client("http://127.0.0.1:7860/")
|
4 |
+
|
5 |
+
# Test case for Code Analysis Report
|
6 |
+
code_for_analysis = "print('Hello, world!')"
|
7 |
+
result_report = client.predict(code=code_for_analysis, api_name="/predict")
|
8 |
+
print("Code Analysis Report result:", result_report)
|
9 |
+
assert (
|
10 |
+
isinstance(result_report, str)
|
11 |
+
and "Analysis Report for provided code:" in result_report
|
12 |
+
)
|
13 |
+
|
14 |
+
# Test case for Code Score
|
15 |
+
code_for_score = "def my_function():\n pass"
|
16 |
+
result_score = client.predict(code=code_for_score, api_name="/predict_1")
|
17 |
+
print("Code Score result:", result_score)
|
18 |
+
assert isinstance(result_score, str) and "Code Score for provided code:" in result_score
|